[ Javascript ] Killer of Sequential Promises

Peter Chang
HackerNoon.com
3 min readDec 3, 2016

--

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:

Chain 4 promises and resolve it recursively:

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.

Solution:

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.

Installation:

Usage:

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--