Waiting for an async function to finish is a normal JavaScript task when you need data from an API, a file, or another promise-based source. The right tool depends on whether you want chained promises, awaited code, or multiple tasks running together.
In practice, await, .then(), and Promise.all() are the main options. They are part of the same promise flow that also powers Promise.resolve() and Promise.reject().
Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.
Method 1: Wait with async and await
await pauses the async function until the promise resolves.
async function getValue() {
return Promise.resolve("async-done");
}
async function run() {
const value = await getValue();
console.log("wait-async:", value);
}
run();You should see one line logging wait-async: async-done.
Use this pattern when you want code that reads in a direct top-to-bottom order.
Method 2: Wait with then()
.then() runs when the promise resolves and is useful when you prefer explicit promise chaining.
Promise.resolve("done")
.then((value) => {
console.log("then-result:", value);
});You should see one line logging then-result: done.
This style is common in older promise-based code and still works well for simple chains.
Method 3: Wait for multiple async tasks with Promise.all()
Promise.all() resolves when every promise succeeds.
Promise.all([Promise.resolve(1), Promise.resolve(2)])
.then((values) => {
console.log("promise-all:", values.join(","));
});You should see one line logging promise-all: 1,2.
Use this when the next step depends on all async results being ready.
Summary
To wait for an async function to finish in JavaScript, use await for readable flow, .then() for promise chaining, or Promise.all() when multiple async tasks should complete together. These are the core patterns for modern promise handling in Node.js and browser code.
