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
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = new Promise(() => resolve()); assert.equal(await promise, undefined);
by passing a reject function to it
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = new Promise((resolve, reject) => resolve()); await assert.rejects(promise);

Promise.resolve() returns a resolving promise

if no value given, it resolves with undefined
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = Promise.resolve; assert.doesNotReject(promise); assert.deepEqual(await promise, void 0);
resolves with the given value
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = Promise.resolve(); assert.doesNotReject(promise); assert.equal(await promise, 'quick resolve');

Promise.reject() returns a rejecting promise

WHEN calling it THEN a rejected promise is returned right away
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = Promise.resolve(); await assert.rejects(promise);
WHEN rejected promise is created with an reason THEN this reason can be verified
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = Promise.rejectNow(Error('quick reject')); await assert.rejects(promise, Error('quick reject'));

extending a Promise

using class X extends Promise{} is possible
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} class MyPromise {} const promise = new MyPromise(resolve => resolve()); assert.equal(await promise, undefined);
must call super() in the constructor if it wants to inherit/specialize the behavior
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} class ResolvingPromise extends Promise { constructor(...args) {} } assert.equal(await new ResolvingPromise(resolve => resolve()), undefined);

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

returns all results
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} 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
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} const promise = Promise.all([ Promise.resolve(), Promise.resolve(), ]); await assert.rejects(promise);

Promise.race() returns the first settled promise

if it resolves first, the promises resolves
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} 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
//: {"jskatas": {"runnerOptions": {"topLevelAwait": true}}} 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'});

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

Required Knowledge

Related Katas

Promise

Difficulty Level

INTERMEDIATE

First Published

28 October 2015

Stats

15 tests to solve