Wait for Async Function to Finish in JavaScript

Learn how to wait for an async function to finish in JavaScript with await, then(), Promise.all(), and practical output examples.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Wait for Async Function to Finish in JavaScript

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.

javascript
async function getValue() {
  return Promise.resolve("async-done");
}

async function run() {
  const value = await getValue();
  console.log("wait-async:", value);
}

run();
Output

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.

javascript
Promise.resolve("done")
  .then((value) => {
    console.log("then-result:", value);
  });
Output

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.

javascript
Promise.all([Promise.resolve(1), Promise.resolve(2)])
  .then((values) => {
    console.log("promise-all:", values.join(","));
  });
Output

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.


Official documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …