

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
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
While the await keyword can be used in place of .then() to resolve a promise, the async keyword can be used in place of new Promise() to create a new promise.
When a function is prefixed with the async keyword, it will automatically return a Promise that resolves to the return value. You can think of async as "wrapping" your function within a promise.
These are equivalent:
function getPromiseForUserData() {
return new Promise((resolve) => {
fetchDataFromServer().then(function (user) {
resolve(user);
});
});
}
const promise = getPromiseForUserData();
async function getPromiseForUserData() {
const user = await fetchDataFromServer();
return user;
}
const promise = getPromiseForUserData();
await can only be used inside an async function or at the top level of a module (file).
In an async function, returning a Promise, will implicitly be awaited by the caller.
Go ahead and try to run the code! You'll get an error.
Update the getMessageHash() function so that it can properly await the promise.
Remember: The sha256Hex is an async function, meaning it returns a Promise and we should await it.