< Back to JavaScript notes

Async Await

Using async/await to write asynchronous JavaScript

Async/Await functions are simple, effective methods for asynchronous JavaScript. They’re essentially functions that will do two additional things things:

An async/await function has a basic structure like this:

async function myAsyncFunction() {
  let value = await promise;
  return value;

const asyncValue = myAsyncFunction;

asyncValue.then( function(value) {

This function will do the following:

  1. It will immediately run, but pause and wait at the await line while the promise resolves
  2. Once the promise resolves, it will continue until the function completes or there’s another await.
  3. When the function is assigned to asyncValue, the variable’s value will be an unresolved promise.
  4. When the variable’s promise resolves, you can treat it like any other resolved promise. In this case, it uses .then to take the returned value when completed and log it.