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.