Async Functions are introduced officially in ECMAScript 7 Spec, as the ultimate way to solve async code writting problems (especially the famous callback hell).
We can easily get the resolved value of a Promise with await
operator.
Syntax
1 | [rv] = await expression; |
expression
A Promise
or any value to wait for.
rv
Returns the fulfilled value of the promise, or the value itself if it’s not a Promise
.
See also: await | MDN
Serial Async Execution
1 | let totalTime = 0; |
The code above will print objects in the original loop order, each timeout
function was executed in serial sequence, resulting the total ellapsed time to be the sum of all loops:
1 | start |
Parall Async Procedure
no matter how much time each one take
1 | async function asyncPrintParall() { |
1 | start |