import delay from 'delay';
import errCode from 'err-code';
export default async function waitFor(test, options) {
  const opts = Object.assign({
    timeout: 5000,
    interval: 0,
    name: 'event'
  }, options);
  const start = Date.now();
  while (true) {
    if (await test()) {
      return;
    }
    if (Date.now() > start + opts.timeout) {
      throw errCode(new Error(`Timed out waiting for ${ opts.name }`), 'ERR_TIMEOUT');
    }
    await delay(opts.interval);
  }
}