diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-01-27 10:24:06 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-01-27 10:24:06 +0100 |
commit | f305d57ab7702c3ca10fd6e32366d19e524ee1f0 (patch) | |
tree | 381ccb09c7cedc72cdf8ad6c98b681b72f4d1bc0 /lib/utils/promise.js | |
parent | 877f2e477b010f9f37a9044606f110a90f077680 (diff) | |
download | gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.zip gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.tar.gz gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.tar.bz2 |
Add more classes structures
Diffstat (limited to 'lib/utils/promise.js')
-rw-r--r-- | lib/utils/promise.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/utils/promise.js b/lib/utils/promise.js new file mode 100644 index 0000000..c25b349 --- /dev/null +++ b/lib/utils/promise.js @@ -0,0 +1,26 @@ +var Q = require('q'); +var _ = require('lodash'); + +// Reduce an array to a promise +function reduce(arr, iter, base) { + return _.reduce(arr, function(prev, elem, i) { + return prev.then(function(val) { + return iter(val, elem, i); + }); + }, Q(base)); +} + +// Transform an array +function serie(arr, iter, base) { + return reduce(arr, function(before, item, i) { + return Q(iter(item, i)) + .then(function(r) { + before.push(r); + return before; + }); + }, []); +} + +module.exports = Q; +module.exports.reduce = reduce; +module.exports.serie = serie; |