Promise in Node.js / JavaScript–Part III

Posted: October 14, 2021 in Javascript, Node.js
Tags: , ,

In the previous post, we have seen Promises can be chained together by returning another Promise object from the onFulfilled parameter of the then function. The chain of Promises executes sequentially. So we can easily build a sequential execution workflow by building a chain of Promises. But before getting into there we will take a detour and examine the Promise.resolve and Array.prototype.reduce functions.

As per documentation, Promise.resolve does the following:

The Promise.resolve(value) method returns a Promise object that is resolved with a given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

Let’s quickly check this out with few code snippets.

image

Here the parameter value is a number and returned promise will be fulfilled with the value as shown below:

image

Next, we will pass a thenable as parameter, thenable is an object with a then function. Note, all promises are thenables but all thenables are not promise.

image

Here the returned Promise object will follow the thenable and execute the then function as shown below:

image

In the code below we are passing a Promise object to the resolve function and it will be fulfilled executing the resolve callback as shown below:

image

imageL

Lastly, we will invoke the resolve function without passing any value.

image

image

So the call to the resolve function without passing any value returns a Promise object which auto resolves without any data ( undefined ). Please note this, as we will use this Promise.resolve() call to initialize our Promise chain while prepare for sequential execution.

As per documentation, Array.prototype.reduce does the following:

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The definition of the reduce function is as follows:

  • reduce(callbackFn)
  • reduce(callbackFn, initialValue)

The callback or reducer function accepts the following arguments:

  • previousValue (the value resulting from the previous call to callback or reducer function)
  • currentValue (the value of the current element)
  • currentIndex [Optional]
  • array [ Optional ]

The following example shows a simple usage of the reduce function to compute sum of the elements of an array of numbers.

image

We will now use the Promise.resolve and Array.prototype.reduce functions to execute an array of promises in sequence.

We have defined three functions returning Promises as shown below.

image

image

image

The array tasks contains the above functions as it’s elements.

image

Now we will chain these promises together using the reduce function as shown below:

image

The execution of this code will print the following:

image

The timings clearly indicates a sequential execution. But we can see that the result of the last task is not printed. This is because the then function is not called on the Promise object that is returned by the reduce function.

If we add the following lines of code we can all results will get printed properly as shown below.

image

image

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.