Creating a Javascript Promise

Poby’s Home
2 min readMar 16, 2022

Reference: Mozilla Promise document

Creating a Promise around an old callback API

A Promise can be created from scratch using its constructor. This should be needed only to wrap old APIs.

Promise() constructor

The Promise constructor is primarily used to wrap functions that do not already support promises.

new Promise(executor)

executor is a function to be executed by the constructor.

function(resolutionFunc, rejectionFunc){
// typically, some asynchronous operation.
}

executor is your code of asynchronous operation, and typically it does not return value. It communicate via the side effect caused byresolutionFunc or rejectionFunc . Inside of the executor function, typically in the callback of asynchronous operation, you call resolutionFunc , or rejectionFunc
Both resolutionFunc or rejectionFunc do not need to defined before the promise. You can define it when you write then or catch

The invocation of resolutionFunc includes a value parameter. The value is passed back to the tethered Promise object. The Promise object (asynchronously) invokes any .then() associated with it. The value received by .then() is passed to the invocation of handleFulfilled as an input parameter

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));wait(1*1000)
.then(() => console.log("1 seconds"))
.catch(() => console.log("failure"));

Modern functions return a promise that you can attach your callbacks to instead.

This is the key point. You attach a callback function later to a promise!

Promise.prototype.then()

The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

Syntax

p.then(onFulfilled[, onRejected]);p.then(value => {
// fulfillment
}, reason => {
// rejection
});

Parameters

onFulfilled Optional
A Function called if the Promise is fulfilled. This function has one argument, the fulfillment value. If it is not a function, it is internally replaced with an “Identity” function (it returns the received argument).
onRejected Optional
A Function called if the Promise is rejected. This function has one argument, the rejection reason. If it is not a function, it is internally replaced with a “Thrower” function (it throws an error it received as an argument).

Return Value

Once a Promise is fulfilled or rejected, the respective handler function (onFulfilled or onRejected) will be called asynchronously (scheduled in the current thread loop). The behavior of the handler function follows a specific set of rules. If a handler function:

  • returns a value, the promise returned by then gets resolved with the returned value as its value.
  • doesn’t return anything, the promise returned by then gets resolved with an undefined value.
  • throws an error, the promise returned by then gets rejected with the thrown error as its value.
  • returns an already fulfilled promise, the promise returned by then gets fulfilled with that promise’s value as its value.
  • returns an already rejected promise, the promise returned by then gets rejected with that promise’s value as its value.
  • returns another pending promise object, the resolution/rejection of the promise returned by then will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the resolved value of the promise returned by then will be the same as the resolved value of the promise returned by the handler.

--

--