We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Async Keyword

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:

New Promise()

function getPromiseForUserData() {
  return new Promise((resolve) => {
    fetchDataFromServer().then(function (user) {
      resolve(user);
    });
  });
}

const promise = getPromiseForUserData();

Async

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.

Assignment

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.