Note: This document makes use of the ECMA Script proposal Asynchronous Blocks.
Consider the following code.
async//do somethingconst result = await //do something asynchronous//do something with result
Because await operates on a promise, it pauses operation and resumes as soon as the promise resolves.
We can use setTimeout to create a promise that resolves after a specified amount of time. Further, there's no reason to capture the result of the promise.
async//do somethingawait /*specified amount of time*///do something with result
We can package this into a module for convenience:
;async//do somethingawait ;//do something with result
It is sometimes useful to use setTimeout without a second argument to force code to execute in the next event loop.
A similar effect can be achieved using delay.
async//do somethingawait ;//do something in the next event loop.