Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Basic JavaScript

JS Tutorial JS Introduction JS Where To JS Output

JS Syntax

JS Syntax JS Statements JS Comments JS Variables JS Let JS Const JS Types

JS Operators

JS Operators JS Arithmetic JS Assignment JS Comparisons JS Conditional JS If JS If Else JS Ternary JS Switch JS Booleans JS Logical

JS Loops

JS Loops JS Loop for JS Loop while JS Break JS Continue JS Control Flow

JS Strings

JS Strings JS String Templates JS String Methods JS String Search JS String Reference

JS Numbers

JS Numbers JS Number Methods JS Number Properties JS Number Reference JS Bitwise JS BigInt

JS Functions

Function Path Function Intro Function Invocation Function Parameters Function Returns Function Arguments Function Expressions Function Arrow Function Quiz

JS Objects

Object Path Object Intro Object Properties Object Methods Object this Object Display Object Constructors

JS Scope

JS Scope JS Code Blocks JS Hoisting JS Strict Mode

JS Dates

JS Dates JS Date Formats JS Date Get JS Date Set JS Date Methods

JS Arrays

JS Arrays JS Array Methods JS Array Search JS Array Sort JS Array Iterations JS Array Reference JS Array Const

JS Sets

JS Sets JS Set Methods JS Set Logic JS Set WeakSet JS Set Reference

JS Maps

JS Maps JS Map Methods JS Map WeakMap JS Map Reference

JS Iterations

JS Loops JS Iterables JS Iterators JS Generators

JS Math

JS Math JS Math Reference JS Math Random

JS RexExp

JS RegExp JS RegExp Flags JS RegExp Classes JS RegExp Metachars JS RegExp Assertions JS RegExp Quantifiers JS RegExp Patterns JS RegExp Objects JS RegExp Methods

JS Data Types

JS Destructuring JS Data Types JS Primitive Data JS Object Types JS typeof JS toString JS Type Conversion

JS Errors

JS Errors Intro JS Errors Silent JS Error Statements JS Error Object

JS Debugging

Debugging Intro Debugging Console Debugging Breakpoints Debugging Errors Debugging Async Debugging Reference

JS Conventions

JS Style Guide JS Best Practices JS Mistakes JS Performance

JS References

JS Statements JS Reserved Keywords JS Operators JS Precedence

JS Versions

JS 2026 JS 2025 JS 2024 JS 2023 JS 2022 JS 2021 JS 2020 JS 2019 JS 2018 JS 2017 JS 2016 JS Versions JS 2015 (ES6) JS 2009 (ES5) JS 1999 (ES3) JS IE / Edge JS History

JS HTML

JS HTML DOM JS Events JS Projects New

JS Advanced

JS Temporal  New JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript async and await

async and await make promises easier

You still use promises, but you write the code like normal step by step code.

async makes a function return a Promise

await makes a function wait for a Promise

Why async and await Exist

Promise chains can become long.

async and await were created to reduce nesting and improve readability.

Promises Example

// Three functions to run in steps
function step1() {
  return Promise.resolve("A");
}
function step2(value) {
  return Promise.resolve(value + "B");
}
function step3(value) {
  return Promise.resolve(value + "C");
}

// Run the three functions in steps
step1()
.then(function(value) {
  return step2(value);
})
.then(function(value) {
  return step3(value);
})
.then(function(value) {
  myDisplayer(value);
});

Try it Yourself »

The same flow with async and await is easier to read.

Example

// Function to run the three functions in steps
async function run() {
  let v1 = await step1();
  let v2 = await step2(v1);
  let v3 = await step3(v2);
  myDisplayer(v3);
}

run();

Try it Yourself »


The async Keyword

The async keyword before a function makes the function return a promise.

This is true even if you return a normal value.

Example

async function myFunction() {
  return "Hello";
}

Is the same as:

function myFunction() {
  return Promise.resolve("Hello");
}

The result is handled with then() because it is a promise:

myFunction().then(
  function(value) { /* code if successful */ },
  function(value) { /* code if some error */ }
);

Example

async function myFunction() {
  return "Hello";
}
myFunction().then(
  function(value) {myDisplayer(value);},
  function(value) {myDisplayer(value);}
);

Try it Yourself »

Or simpler, since you expect a normal value (a normal response, not an error):

Example

async function myFunction() {
  return "Hello";
}
myFunction().then(
  function(value) {myDisplayer(value);}
);

Try it Yourself »


The await Keyword

The await keyword makes a function pause the execution and wait for a resolved promise before it continues:

let value = await promise;

The await keyword can only be used inside an async function:

Example

function step1() {
  return Promise.resolve("A");
}

async function run() {
  let value = await step1();
  myDisplayer(value);
}

run();

Try it Yourself »



Handling Errors with try...catch

Promises use catch() for errors.

async and await use try...catch.

Example

function fail() {
  return Promise.reject("Failed");
}

async function run() {
  try {
    let value = await fail();
    console.log(value);
  } catch (error) {
    console.log(error);
  }
}

run();

Note

Errors from awaited promises are caught like normal errors.


Sequential vs Parallel

Awaiting one by one runs tasks in sequence.

This is correct when one step depends on the previous step.

Example

async function run() {
  let a = await step1();
  let b = await step2();
  console.log(a, b);
}

If tasks do not depend on each other, you can run them in parallel.

Use Promise.all() to wait for both.

Example

async function run() {
  let p1 = step1();
  let p2 = step2();
  let values = await Promise.all([p1, p2]);
  console.log(values);
}

Note

Start the promises first.

Await them together.


Real Example with fetch

fetch() returns a promise.

This makes it a perfect example for async and await.

Example

async function loadData() {
  try {
    let response = await fetch("data.json");
    let data = await response.json();
    console.log(data);
  } catch (error) {
    console.log(error);
  }
}

loadData();

This is promise-based async code written in a synchronous style.


Common Beginner Mistakes

Using await outside an async function causes an error.

Forgetting try...catch can hide async errors.

When async code fails, check the console and the Network tab.


More Examples

Basic Syntax

async function myDisplay() {
  let myPromise = new Promise(function(resolve, reject) {
    resolve("I love You !!");
  });
  document.getElementById("demo").innerHTML = await myPromise;
}

myDisplay();

Try it Yourself »

The two arguments (resolve and reject) are pre-defined by JavaScript.

We will not create them, but call one of them when the executor function is ready.

Very often we will not need a reject function.

Example without reject

async function myDisplay() {
  let myPromise = new Promise(function(resolve) {
    resolve("I love You !!");
  });
  document.getElementById("demo").innerHTML = await myPromise;
}

myDisplay();

Try it Yourself »

Waiting for a Timeout

async function myDisplay() {
  let myPromise = new Promise(function(resolve) {
    setTimeout(function() {resolve("I love You !!");}, 3000);
  });
  document.getElementById("demo").innerHTML = await myPromise;
}

myDisplay();

Try it Yourself »


Browser Support

async/await is an ECMAScript 2017 feature.

ES2017 is supported in all modern browsers since September 2017:

Chrome
58
Edge
15
Firefox
52
Safari
11
Opera
45
Apr 2017 Apr 2017 Mar 2017 Sep 2017 May 2017

Next Chapter

The next page focuses on fetch() and real-world network requests.

You will learn status codes, JSON parsing, and common fetch mistakes.

The Fetch API


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->