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 Promise Reference

This page is a reference for JavaScript promises.

Use it to look up promise methods and common patterns.

Note

If you want to learn promises step by step, read JavaScript Promises.


The Promise Object

The Promise Object represents the completion or failure of an asynchronous operation.

A Promise can have 3 states:

pendinginitial state
rejectedoperation failed
fulfilledoperation completed

Promise Object Methods

Revised December 2025


Instance Methods

Method Description
.catch(onRejected) Provides a function to run when a promise is rejected
.finally(onFinally) Provides a function to run when a promise is fulfilled or rejected
.then(onFulfilled, onRejected) Provides two functions to run when a promise is fulfilled

Static Methods

Method Description
Promise.all() Returns a single Promise from a list of promises
When all promises fulfill
Promise.allSettled() Returns a single Promise from a list of promises
When all promises sette
Promise.any() Returns a single Promise from a list of promises
When any promise fulfills
Promise.race() Returns a single Promise from a list of promises
When the faster promise settles
Promise.reject() Returns a Promise rejected with a given value
Promise.resolve() Returns a Promise resolved with a given value
Promise.then() Provides two callbacks:
One funtion to run when a promise is fulfilled.
One funtion to run when a promise is rejected.
Promise.try() Executes a function and wraps its result in a promise.
Promise.withResolvers() Returns an object containing a new Promise object and two functions to resolve or reject it.

The .then() Method

The .then() method provides two functions to run when a promise is fulfilled.

Example

// Using then() to display a result
myPromise.then(
  function(value) {myDisplayer(value)},
  function(value) {myDisplayer(value)}
);

Try it Yourself »


The .catch() Method

The .catch() method provides a function to run when a promise is rejected.

Example

// Using catch() to display a result
myPromise.catch(
  function(value) {myDisplayer(value);}
);

Try it Yourself »


The .finally() Method

The .finally() method provides a function to run when a promise is fulfilled or rejected.

Example

// Using finally() to display a result
myPromise.finally(
  function(value) {myDisplayer(value)}
);

Try it Yourself »


Promise.resolve()

The Promise.resolve() method returns a Promise object resolved with a value.

Example

let promise = Promise.resolve("Resolved OK");

promise
.then(function(value) {
  myDisplayer(value);
})
.catch(function(value) {
  myDisplayer(value);
});

Try it Yourself »


Promise.reject()

The Promise.reject() method returns a Promise object rejected with a value.

Example

let promise = Promise.reject("Something went wrong");

promise
.then(function(value) {
  myDisplayer(value);
})
.catch(function(value) {
  myDisplayer(value);
});

Try it Yourself »


Promise.all()

The Promise.all() method returns a single Promise from a list of promises, when all promises fulfill (or rejects when one promise rejects).

Example

let p1 = Promise.resolve("A");
let p2 = Promise.resolve("B");

Promise.all([p1, p2])
.then(function(values) {
  myDisplayer(values);
});

Try it Yourself »


Promise.allSettled()

Promise.allSettled() returns an array of results for all promises.

Each result contains a status and a value or reason.

Examples

let p1 = Promise.resolve("A");
let p2 = Promise.reject("X");

Promise.allSettled([p1, p2])
.then(function(results) {
  myDisplayer(JSON.stringify(results));
});
Try it Yourself »
// Create a Promise
const myPromise1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 200, "King");
});

// Create another Promise
const myPromise2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, "Queen");
});

// Settle All
Promise.all([myPromise1, myPromise2])
.then(function(values) {
  myDisplayer(values);
});
Try it Yourself »

Note

Promise.allSettled() means "Just run all promises. I don't care about the results".

Browser Support

Promise.allSettled() is an ES2020 feature.

ES2020 is fully supported in all modern browsers since September 2020:

Chrome
85
Edge
85
Firefox
79
Safari
14
Opera
71
Aug 2020 Aug 2020 Mar 2020 Sep 2020 Sep 2020

Promise.race()

Promise.race() settles when the first promise settles.

Example

let p1 = new Promise(function(resolve) {
  setTimeout(function() { resolve("First"); }, 500);
});
let p2 = new Promise(function(resolve) {
  setTimeout(function() { resolve("Second"); }, 1000);
});

Promise.race([p1, p2])
.then(function(value) {
  myDisplayer(value);
});
Try it Yourself »

Promise.any()

Promise.any() resolves when the first promise is fulfilled.

It rejects only if all promises reject.

Example

let p1 = new Promise(function(resolve) {
  setTimeout(function() { resolve("First"); }, 500);
});
let p2 = new Promise(function(resolve) {
  setTimeout(function() { resolve("Second"); }, 1000);
});

Promise.race([p1, p2])
.then(function(value) {
  myDisplayer(value);
});
Try it Yourself »

Promise.any() is useful when multiple sources can provide the same result.



JavaScript Promise.withResolvers()

Promise.withResolvers() is a static method that simplifies the creation and management of Promises.

Promise.withResolvers() provides a more convenient way to access the resolve and reject functions associated with a Promise outside of its executor function.

Instead of the traditional new Promise((resolve, reject) => { ... }) constructor pattern, Promise.withResolvers() returns an object containing:

  • promise: The newly created Promise instance
  • resolve: A function to fulfill the promise with a value
  • reject: A function to reject the promise with a reason (error)

Example

const {promise, resolve, reject} = Promise.withResolvers();

// You can now use 'resolve' and 'reject' anywhere in your code to control the state of the "promise".

// Simulate async work
setTimeout(() => {
  const success = Math.random() > 0.5;
  if (success) {
    resolve("Operation successful!");
  } else {
    reject("Operation failed!");
  }
}, 1000);

// Display result the promise finishes
promise
.then((value) => {
  myDisplayer(value);
})
.catch((value) => {
  myDisplayer(value);
});
Try it Yourself »

Example Explained

  • After 1 second, the promise resolves or rejects
  • The result is written into "demo"

The code to simulate async work can be simplified to:

// Simulate async work
setTimeout(() => {
  Math.random() > 0.5
  ? resolve("Operation successful!")
  : reject("Operation failed!");
}, 1000);

The then / catch code can be simplified to:

// Set text in then/catch, update DOM in finally
promise
.then((message) => text = message)
.catch((error) => text = error)
.finally(() => {myDisplayer(text)}
);
Try it Yourself »

Example Explained

  • .then() or .catch() set the text variable
  • .finally() always runs last, no matter success or failure
  • The DOM is updated exactly once, cleanly and reliably

Using async/await is the cleanest:

// Use async/await to handle the promise
(async () => {
  try {
    text = await promise; // Wait for resolve
  } catch (err) {
    text = err; // Handle reject
  }
  // Update the UI after promise finishes
  myDisplayer(text);
})();
Try it Yourself »

Example Explained

  • async/await makes asynchronous code look synchronous
  • The DOM is updated after the promise resolves or rejects
  • The UI updates in one place (no duplicated innerHTML calls)
×

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.

-->