Categories
JavaScript Answers

How to use async await and .then together with JavaScript?

Spread the love

To use async await and .then together with JavaScript, we replace then with await.

For instance, we write

const f = async () => {
  try {
    const response = await fetch("http://no-such-url");
  } catch (err) {
    alert(err);
  }
};

to call fetch to return a promise with the response.

And we use await to get the response from the promise.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *