Join-Generator for Node.js
What is this?
Join Generator provides a simple API for joining multiple threads of control, and supports callback and promises style.
A Generator
is created like so:
var Generator = Generator; var gen = { // executed when all threads have finished, or when // an error has been triggered. console;};
The passed function is the triggered action when all gates are released. It accepts an error argument. If any of the gates are passed an error (or any truthy argument), the triggered function will be called immediately.
The Generator object has two methods: gate
and release
.
-
.gate ([error])
- Returns a callback accepting an optional error argument. When the last issued callback is called, the generator calls its triggered function.
-
.release ([error])
- Manually releases the triggered function. All active gates are ignored.
To continue the example:
var a = gen;var b = gen;var c = gen; ; ; ; // 'all done' will be printed after 1200 milliseconds.
Joiner
's are like Generator
's, but for Promises.
A Joiner is created like this:
var j = new Joiner({collect: true});
The constructor accepts an options object. Valid options are:
collect
Boolean- Whether or not to collect the resolved promise values added to this
Joiner
.
- Whether or not to collect the resolved promise values added to this
Joiner
's have one method: add
.
.add (...promises)
- Add some promises to this Joiner. Each promise added will block this Joiner from resolving until all promises resolve, or any reject.