

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Synchronous vs. Asynchronous
incomplete
2: Why Async?
incomplete
3: Promises
incomplete
4: Why Promises?
incomplete
5: Await
incomplete
6: Async Keyword
incomplete
7: then vs. await
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
The await keyword is used to wait for a Promise to resolve. Once it has been resolved, the await expression returns the value of the resolved promise. It's basically a more modern syntax for .then callbacks.
promise.then((message) => {
console.log(`Resolved with ${message}`);
});
const message = await promise;
console.log(`Resolved with ${message}`);
Personally, I recommend using await over .then whenever possible. It's cleaner and easier to read.
When using await, if the promise is rejected, it will throw an error. That means we can use standard try/catch blocks to handle rejections.
try {
const message = await promise;
console.log(`Resolved with ${message}`);
} catch (error) {
console.log(`Rejected with ${error}`);
}
Similar to before, the updateMessageStatus function takes a message id, current status and delivered state and returns a Promise.
On line 1, call updateMessageStatus with inputs:
messageId = "M123"currentStatus = "Sending"isDelivered = trueThen on line 2, await the returned promise and save the resolved value in a variable called message which will be logged to the console (which is already written on line 5).
For extra difficulty, try combining lines 1 and 2 into one line by awaiting the output of updateMessageStatus.