Hackernoon logo[ Javascript ] Killer of Sequential Promises by@peterchang_82818

[ Javascript ] Killer of Sequential Promises

The pain of javascript is fighting with async functions, specially the big boss “CALLBACK HELL”, when you putting thousand console.log to make the code dirty to kill one CALLBACK bug. It lasted for long until I found Promises, which follows simple logical, organized resolve-reject structure and it solves most of the async problems, so thank you to Promises.

However, a use case that chain N unknown number of promises with parameters and resolving them sequentially, it seem like impossible to accomplish by either native ES6 Promises or by third party libraries: q, bluebird, deferred.js etc.

Failed example:

Defining genPromist function which will be used many times in this article:
var genPromist = function(para) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('para: ', para)
resolve(para * para)
}, 1000)
Chain 4 promises and resolve it recursively:
var promises = [];
var recursiveFunction = function(promisesList) {
return new Promise(function(resolve, reject) {
if (promisesList.length <= 0) {
} else {
return promisesList[0].then(function() {
return recursiveFunction(promisesList);
Sadly, the program did not run as expected, and the promises array was resolved parallelly. And most of the libraries works well when promise function has no parameter, which means that a CLASS in javascript.


The ways Javascript handling functions of promises and functions of promises with parameters are different ( issue of anonymous function with parameters ).

So that handling this issue needed to push promise function (not a promise) to an array list
PACU is a tool to solve the problem of chaining promises functions with parameters and resolving each continuously.


$ npm install --save pacu


var pacu = require("pacu")
pacu.series(promisesList).then(function(result) {
  console.log("Result series: ", result)
  console.log("Error series: ", result)


Join Hacker Noon

Create your free account to unlock your custom reading experience.