async and await
We have just seen the Promise
syntax. With the async
keyword, we can make a function return a Promise. This makes the Promises nicer to read and look a lot like synchronous (non-concurrent) code. We can use this Promise just like we learned in the previous section, or we can use the more powerful await
keyword to wait until the Promise is done. await
only works in an asynchronous function.
In an asynchronous context, we can await other Promises as well, as can be seen in this example:
function saySomething(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve("something" + x);
}, 2000);
});
}
async function talk(x) {
const words = await saySomething(x);
console.log(words);
}
talk(2);
talk(4);
talk(8);
Can you figure out what this code does? We call the asynchronous function talk()
three times in a row with no break. Each of these function calls is awaiting the saySomething()
function...