Wait for Async Function to Finish in JavaScript

Tech reviewed: Deepak 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: 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.

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

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

run();

Tested output:

text
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);
  });

Tested output:

text
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(","));
  });

Tested output:

text
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 user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design