-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise-chain.js
131 lines (116 loc) · 3.61 KB
/
promise-chain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @file Manages a chain of promises with serial and paralell capabilities.
* @copyright Ben Mackie 2015
* @license MIT
*/
'use strict';
var Promise = require('bluebird');
var PromiseFork = require('./promise-fork');
/**
* @class
*/
function PromiseChain(parent) {
var _this = this;
if (parent && typeof parent.then != 'function')
throw new Error('Argument specified not a promise.');
var _promise = parent || Promise.resolve();
var _fork = null;
/**
* Add a promiser style function (a functions that returns a promise) to the chain.
*/
_this.addPromiser = function(promiser) {
if (_fork)
_fork.addPromiser(promiser);
else
_promise = _promise.then(promiser);
return _this;
}
/**
* Add a collection of promiser style functions (a functions that returns a promise)
* to the chain, to run in parallel and join as an 'all'.
* This will be the case whether the promise chain has been forked or not.
*/
_this.addPromisers = function(promisers) {
return _this.addPromiser(function() {
var promises = [];
promisers.forEach(function(promiser) {
promises.push(promiser());
})
return Promise.all(promises);
});
}
/**
* Add a resolver style function (a functions that has resolver and reject callback
* arguments respectively) to the chain.
*/
_this.addResolver = function(resolver) {
return _this.addPromiser(function() { return new Promise(resolver); });
}
/**
* Adds a collection of resolver style functions (a functions that has resolver and reject
* callback arguments respectively) to the chain, to run in parallel and join as an 'all'.
* This will be the case whether the promise chain has been forked or not.
*/
_this.addResolvers = function(resolvers) {
return _this.addPromiser(function() {
var promises = [];
resolvers.forEach(function(resolver) {
promises.push(new Promise(resolver));
})
return Promise.all(promises);
});
}
/**
* Maps one or more functions to items in a collection in order to yield a collection of
* promisers to add to the chain.
* @param collection - the collection to enumerate.
* @param promiser - the promiser
*/
/*
_this.mapPromisers = function(collection, promiserFactory) {
// Loop over items in collection and add promiser.
collection.forEach(function(item) {
promisers.forEach(function(promiser) {
_this.addPromiser(function() { return promiserFactory(item); });
})
})
return _this;
}
*/
_this.fork = function() {
_fork = new PromiseFork(_promise);
return _this;
}
_this.join = function() {
if (_fork) {
var fork = _fork;
_fork = null;
_promise = fork.join();
}
return _this;
}
_this.joinAny = function() {
if (_fork) {
var fork = _fork;
_fork = null;
_promise = fork.joinAny();
}
return _this;
}
_this.joinSettle = function() {
if (_fork) {
var fork = _fork;
_fork = null;
_promise = fork.joinSettle();
}
return _this;
}
_this.promise = function() {
return _promise;
}
_this.then = function() {
this.join();
return _promise.then.apply(_promise, arguments);
}
};
module.exports = PromiseChain;