jskatas.org Continuously Learn JavaScript. Your Way.

Promise: creation

A promise can be created in multiple ways, learn them all here.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

A promise can be created in multiple ways

creating a promise fails when

  • using Promise as a function
  • no parameter is passed
  • passing a non-callable throws too

most commonly Promises get created using the constructor

  • by passing a resolve function to it
  • by passing a resolve and a reject function to it

extending a Promise

  • using class X extends Promise{} is possible
  • must call super() in the constructor if it wants to inherit/specialize the behavior

Promise.all() returns a promise that resolves when all given promises resolve

  • returns all results
  • is rejected if one rejects

Promise.race() returns the first settled promise

  • if it resolves first, the promises resolves
  • if one of the given promises rejects first, the returned promise is rejected

Promise.resolve() returns a resolving promise

  • if no value given, it resolves with undefined
  • resolves with the given value

Promise.reject() returns a rejecting promise

  • if no value given, it rejects with undefined
  • the parameter passed to reject() can be used in the .catch()

Links

Describing the promise constructor.
How `Promise.all()` is specified.
Documenting `Promise.all()`.
How `Promise.race()` is specified.
Documenting `Promise.race()`.
How `Promise.resolve()` is specified.
Documenting `Promise.resolve()`.
How `Promise.resolve()` is specified.
Documenting `Promise.reject()`.