Wait Group
Lose implementation of Golang's WaitGroup
. It offers a different, very simple, way of working with async control flows.
Its strength is its simplicity. It offers no help dealing with errors or data as such (you have to use scope for that). In return you get a ridiculously simple interface.
Install & import
npm install wait-group
const waitGroup =
Usage
The module has a very simple interface. It only exposes one funcion, taking no arguments, that will give you an instance of a waitGroup
. The waitGroup
's exposed functions are:
wg.add(n : Number)
: Call this when you start a process that thewaitGroup
has to wait for.n
is usually just1
and must be above0
. If it is called without arguments, it is assumed thatn = 1
.wg.done()
: Call this when an added function is done. For instance when a callback returns or a promise resolves (or rejects).wg.wait(fn : function)
: Functions added with this will be called whenever all that were added are done. If the waitGroup instance is aleady completed, then it will be called immediately; much like you'd expect.then()
on a promise to behave.
Examples
// Very simple, but it uses the entire interfacevar wg = wg wg
// Here we run three requests in parallel, and handle the result.var results = {}var error = nullvar wg = wg // firstwg // secondwg // thirdwg
License
It's less than 50 lines of code. Why are you reading this?
MIT