Promise

An object representing the result of an operation that will be available in the future: a value on success or an error.

Returns
A new Promise object in the pending state.
On this page

Example

#
JavaScript
const promise = new Promise((resolve) => {
  console.log('1. Executor starts immediately')
  setTimeout(() => resolve('3. Result after 300 ms'), 300)
})

promise.then((value) => console.log(value))
console.log('2. Code after creating the promise')
Console
1. Executor starts immediately
2. Code after creating the promise
3. Result after 300 ms

Definition and usage

#

A promise (Promise) is an object that stands in for the result of an operation that isn’t ready yet, such as fetching data from a server. It starts in the pending state (pending), then permanently moves into one of two states: fulfilled (fulfilled) with a value, or rejected (rejected) with a reason.

The new Promise(executor) constructor takes a function that receives two other functions: resolve to report success and reject to report an error. The executor function runs immediately, synchronously, the moment the promise is created. You react to the result with the then(), catch(), and finally() methods, or with the await keyword.

A promise settles only once. Further calls to resolve or reject are ignored. Functions passed to then() always run asynchronously, even when the promise is already fulfilled, so code written after them runs first.

In practice, you rarely create promises by hand. Built-in APIs such as fetch() return them, and so does every async function. The constructor is mostly useful for wrapping older callback-based code, such as setTimeout().

Syntax

#
Syntax
new Promise((resolve, reject) => { … })

Parameters

#
  • executor

    function (resolve, reject)

    The function that runs immediately when the promise is created. Call resolve(value) inside it on success, or reject(error) on failure.

More examples

#
Success and error
JavaScript
const checkAnswer = (answer) =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      if (answer === 42) resolve('Correct answer!')
      else reject(new Error('Try again'))
    }, 200)
  })

console.log('Checking...')
checkAnswer(42).then((message) => console.log(message))
checkAnswer(7).catch((error) => console.log(error.message))
Console
Checking...
Correct answer!
Try again
Wrapping setTimeout() in a promise
JavaScript
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

console.log('Loading the level...')
await wait(500)
console.log('Level loaded')
Console
Loading the level...
Level loaded
A promise settles only once
JavaScript
const promise = new Promise((resolve, reject) => {
  resolve('first result')
  resolve('second result')
  reject(new Error('too late'))
})

promise.then((value) => console.log(value))
Console
first result

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always handle a promise’s error with catch() or try...catch with await. An unhandled rejection shows up in the console as an "Uncaught (in promise)" error.
  • Pass an Error object to reject(), not a plain string. That way you get a message in message, an error name, and a call stack.
  • Instead of long then() chains, new code is usually more readable with async and await.

Related entries

#

Spotted a mistake or missing an example? Write to us.