Skip to content

Promises

What is a Promise

A Promise is an object that appeared in the ECMAScript 6 specification, released in 2015 (ES6 or ES2015). Promises were used long before thanks to polyfills, notably Bluebird and Q. I recommend using native promises, and occasionally, but rarely and in very specific cases, using Bluebird.

A promise is an object that can be in one of two states:

  • pending
  • settled

If it is settled, it can be in one of two sub-states:

  • fulfilled
  • rejected

Once settled, it cannot transition to another state.

Using Promises

A promise is an object with 3 properties:

  • then
  • catch
  • finally (Available since ES2018)

All three are functions.

Promise.prototype.then is a function that takes one or two (I recommend using only one) callback functions as parameters: the first is executed if the promise is fulfilled with one argument: the resolved value, the second is executed if the promise is rejected, with one argument: the thrown error.

Promise.prototype.catch() is a function that takes a single function as a parameter, and this function will receive one argument: the thrown error, and it will only be executed if the promise is rejected.

Promise.prototype.finally() is a function that also takes a single function as a parameter, to which no arguments will be passed, and it will be executed in all cases.

These three methods return promises, making them chainable:

js
asyncFunction()
  .then(val => val * 2) // Will only be executed if asyncFunction() returns a fulfilled promise
  .catch(err => console.error(err)) // Will only be executed if asyncFunction() OR .then(val => val * 2) returns a rejected promise
  .finally(() => cleanupFunction()) // Will be executed in all cases

The async/await Keywords

It is possible to write the previous code in another, less verbose way:

js
(async function () {
  try {
    const val = await asyncFunction()
    return val * 2
  }
  catch (error) {
    console.error(error)
  }
  finally {
    cleanupFunction()
  }
})()