Introduction

suggest change

A function defined as async can pause on await until a Promise is fulfilled or rejected. The function still returns a promise immediately. fetch rejects on network failure, not on an HTTP error status, so check response.ok before reading the body.

For instance, using the promise-based Fetch API:

async function getJSON(url) {
    try {
        const response = await fetch(url);
        return await response.json();
    }
    catch (err) {
        // Rejections in the promise will get thrown here
        console.error(err.message);
    }
}

An async function always returns a Promise itself, so you can use it in other asynchronous functions.

Arrow function style

const getJSON = async url => {
    const response = await fetch(url);
    return await response.json();
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents