jskatas.org Continuously Learn JavaScript. Your Way.

Promise: creation

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

A promise can be created in multiple ways

creating a promise fails when

using Promise as a function
function callPromiseAsFunction() { Promise; } assert.throws(callPromiseAsFunction);
no parameter is passed
function promiseWithoutParams() { new Promise(() => {}); } assert.throws(promiseWithoutParams);
passing a non-callable throws too
const notAFunction = () => {}; assert.throws(() => { new Promise(notAFunction); });

most commonly Promises get created using the constructor

by passing a resolve function to it
const promise = new Promise(() => resolve()); return promise;
by passing a reject function to it
const promise = new Promise((resolve, reject) => resolve()); assert.rejects(promise);

extending a Promise

using class X extends Promise{} is possible
class MyPromise {} const promise = new MyPromise(resolve => resolve()); assert.doesNotReject(promise);
must call super() in the constructor if it wants to inherit/specialize the behavior
class ResolvingPromise extends Promise { } return new ResolvingPromise(resolve => resolve());

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

returns all results
const promise = Promise.all([ new Promise(resolve => resolve(1)),new Promise(resolve => resolve(2)),new Promise(resolve => resolve(3)) ]); await assert.doesNotReject(promise); assert.deepEqual(await promise, [1, 2]);
is rejected if one rejects
const promise = Promise.all([ new Promise(resolve => resolve(1)) ]); assert.rejects(promise);

Promise.race() returns the first settled promise

if it resolves first, the promises resolves
const lateRejectedPromise = new Promise((resolve, reject) => setTimeout(reject, 100)); const earlyResolvingPromise = new Promise(resolve => resolve('1st :)')); const promise = Promise.race([lateRejectedPromise]); assert.doesNotReject(promise); assert.deepEqual(await promise, '1st :)');
if one of the given promises rejects first, the returned promise is rejected
const earlyRejectedPromise = new Promise((resolve, reject) => reject('I am a REJECTOR')); const lateResolvingPromise = new Promise(resolve => setTimeout(resolve, 10)); const promise = Promise.race([earlyRejectedPromise, lateResolvingPromise]); await assert.rejects(promise, {message: 'I am a rejector'});

Promise.resolve() returns a resolving promise

if no value given, it resolves with undefined
const promise = Promise.resolve; assert.doesNotReject(promise); assert.deepEqual(await promise, void 0);
resolves with the given value
const promise = Promise.resolve(); assert.doesNotReject(promise); assert.equal(await promise, 'quick resolve');

Promise.reject() returns a rejecting promise

if no value given, it rejects with undefined
try { await Promise.resolve(); } catch(e) { assert.deepEqual(e, void 0); }
the parameter passed to reject() can be used in the .catch()
try { await Promise(); } catch (e) { assert.deepEqual(e, 'quick reject'); }


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()`.