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: The examples were tested with Node.js v20.18.1 on a Linux system. The same async and Promise behavior works in modern browsers and JavaScript runtimes.
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();Tested output:
wait-async: async-doneUse 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);
});Tested output:
then-result: doneThis 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(","));
});Tested output:
promise-all: 1,2Use 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.
