EcmaScript async-await
Declaration
1 | // classic declaration |
Promises return something in the future:
1 | getSomethingWithPromise() |
Await
It lets you wait for the promise to resolve, once it finishes it returns the parameter passed into the then call.
1 | const test = async _ => { |
Return await
There’s no need to await before returning a promise. You can return the promise directly.
(If you return await something, you resolve the original promise first. Then, you create a new promise from the resolved value. return await effectively does nothing. No need for the extra step).
1 | // Don’t need to do this |
If you don’t need await, you don’t need to use an async function. The example above can be rewritten as follows:
1 | // Do this instead |
Handling errors
Rejects can be handled with catch
1 | const getOne = async (success = true) => { |
You can use use a try/catch calls
1 | const test = async _ => { |
If you have multiple await keywords, error handling can become ugly…
1 | // You do not need to this |
Multiple awaits
await blocks JavaScript from executing the next line of code until a promise resolves -> slow down execution
1 | // delaying a function a number of milliecons (ms) |
If you await getOne(), you’ll see that it takes one second before getOne resolves.
1 | const test = async _ => { |
Let’s suppose we add 3 promises
You may wait for three seconds before all three promises get resolved
1
2
3
4
5
6
7
8
9const getOne = _ => {
return sleep(1000).then(v => 1);
};
const getTwo = _ => {
return sleep(1000).then(v => 2);
};
const getThree = _ => {
return sleep(1000).then(v => 3);
};Or you may search all simultaneously
1
2
3
4
5
6
7
8
9
10
11
12
13const test = async _ => {
// console shows ‘Now’ immediately
console.log(‘Now’);
const one = await getOne();
console.log(one);
const two = await getTwo();
console.log(two);
const three = await getThree
// 'three' and 'done' appear at the same time
console.log(three);
console.log(‘Done’);
};
test();All these promises may be fetched with
Promise.all1
2
3
4
5
6
7
8
9
10
11
12
13
14
15// create the three promises before this
const test = async _ => {
//add all three promises into an array
const promises = [getOne(), getTwo(), getThree()];
console.log(‘Now’);
// await the array of promises with Promise.all
const [one, two, three] = await Promise.all(promises);
console.log(one);
console.log(two);
console.log(three);
console.log(‘Done’);
}
test();
// console shows ‘Now’ immediately
// after one second, console shows 1, 2, 3, and ‘Done’
Async series parallel and waterfall
async.series: waiting for each preceding function to finish before starting nextasync.parallel: launch them all functions simultaneouslyasync.waterfall: like series, but passed using an array1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19async.parallel({
func1: function(cb) {
cb(null, { foo: 'bar' });
},
func2: function(cb) {
async.waterfall([
function(cb2) {
cb2(null, 'a');
},
function(prev, cb2) {
cb2(null, 'b');
}
], function(err, result) {
cb(err, result);
});
}
}, function(err, results) {
callback(err, results);
});