Start here.
async marks a function whose result is delivered through a Promise. Even if the body returns a plain value, calling an async function gives a promise fulfilled with that value. await pauses that function's own continuation until a promise settles. It does not freeze the entire browser or stop other events. When the awaited promise fulfills, its value becomes the value of the await expression.
Reading async code can feel more linear: const response = await fetch(url); follows the request and names the eventual response. The next line runs only after that promise fulfills. If the promise rejects, await throws inside the async function, so try and catch can handle it. The function still returns a promise to its own caller, which must also await it or attach a handler.
Several independent requests can be started together and combined with Promise.all rather than awaited one by one. Sequential awaits are right when the second operation needs the first result. Ask which work depends on which value. async and await make the flow easier to read, but they do not change the timing rules of promises or make network work reliable by themselves.
Build the model.
An async function always returns a promise. await pauses that function's continuation until a promise settles; it does not freeze the entire browser. This lets dependent steps read top to bottom while the event loop remains free to handle other work.
A rejected awaited promise throws at the await point, so ordinary try and catch can handle it. Independent operations need not always wait one after another: start them together and use Promise.all when both results are required. Sequential awaits are right when the second step depends on the first.
How it works.
An async function always returns a promise. Within it, await evaluates a promise-like value and suspends only that function's continuation until the result is ready. Other browser events and tasks can proceed in the meantime. On fulfillment, await produces the resolved value; on rejection, it throws at that line so a surrounding try/catch can handle it.
Use sequential awaits when the second operation needs a result from the first. If two operations are independent, start both before waiting and use Promise.all to collect their results. Promise.all rejects if any member rejects, but it does not automatically cancel the others. The distinction between dependent and independent work is the key to writing asynchronous code that is both clear and efficient.
Read it line by line.
async function loadPage() {
const profile = Promise.resolve({ name: 'Ada' });
const messages = Promise.resolve(['Hello']);
const [user, notes] = await Promise.all([profile, messages]);
console.log(user.name, notes[0]);
}
loadPage();First notice. Both operations begin before the function waits for their combined result.
Calling loadPage starts an async function. profile and messages hold two promises, each already fulfilled with a value. Promise.all produces a promise for both results. await pauses loadPage until that promise settles, then array destructuring assigns the profile object to user and the message array to notes. The console displays Ada and Hello. If either promise rejected, the await expression would throw inside loadPage.
See the rule again.
const loadUser = async () => ({ id: 7 });
const loadPosts = async id => [`Post for ${id}`];
async function loadUserPosts() {
const user = await loadUser();
return loadPosts(user.id);
}
loadUserPosts().then(console.log);The output is ['Post for 7']. loadPosts needs user.id, so it must wait for loadUser. The async function returns a promise for the posts even though it returns the loadPosts promise directly; JavaScript adopts that result. If either operation rejects, loadUserPosts rejects unless a caller handles the failure.
One thing to remember.
await expresses a dependency; use it where a result is actually needed.