Skip to content

Instantly share code, notes, and snippets.

@aackerman
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save aackerman/8e8f50d143f45ed93d5b to your computer and use it in GitHub Desktop.

Select an option

Save aackerman/8e8f50d143f45ed93d5b to your computer and use it in GitHub Desktop.
Async timeout promise
let delay = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, timeout);
});
};
export default delay;

For use in testing instead of a setTimeout callback.

it('will be called later', async function(done) {
  let now = Date.now();
  await delay(500);
  // timing will probably be off by a few milliseconds
  expect(Date.now() - now).toBeAbout(500);
  done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment