diff options
Diffstat (limited to 'lib/utils/promise.js')
-rw-r--r-- | lib/utils/promise.js | 112 |
1 files changed, 96 insertions, 16 deletions
diff --git a/lib/utils/promise.js b/lib/utils/promise.js index d49cf27..19d7554 100644 --- a/lib/utils/promise.js +++ b/lib/utils/promise.js @@ -1,19 +1,46 @@ var Q = require('q'); -var _ = require('lodash'); +var Immutable = require('immutable'); -// Reduce an array to a promise +/** + Reduce an array to a promise + + @param {Array|List} arr + @param {Function(value, element, index)} + @return {Promise<Mixed>} +*/ function reduce(arr, iter, base) { - return _.reduce(arr, function(prev, elem, i) { + arr = Immutable.Iterable.isIterable(arr)? arr : Immutable.List(arr); + + return arr.reduce(function(prev, elem, key) { return prev.then(function(val) { - return iter(val, elem, i); + return iter(val, elem, key); }); }, Q(base)); } -// Transform an array +/** + Iterate over an array using an async iter + + @param {Array|List} arr + @param {Function(value, element, index)} + @return {Promise} +*/ +function forEach(arr, iter) { + return reduce(arr, function(val, el, key) { + return iter(el, key); + }); +} + +/** + Transform an array + + @param {Array|List} arr + @param {Function(value, element, index)} + @return {Promise} +*/ function serie(arr, iter, base) { - return reduce(arr, function(before, item, i) { - return Q(iter(item, i)) + return reduce(arr, function(before, item, key) { + return Q(iter(item, key)) .then(function(r) { before.push(r); return before; @@ -21,9 +48,17 @@ function serie(arr, iter, base) { }, []); } -// Iter over an array and return first result (not null) +/** + Iter over an array and return first result (not null) + + @param {Array|List} arr + @param {Function(element, index)} + @return {Promise<Mixed>} +*/ function some(arr, iter) { - return _.reduce(arr, function(prev, elem, i) { + arr = Immutable.List(arr); + + return arr.reduce(function(prev, elem, i) { return prev.then(function(val) { if (val) return val; @@ -32,8 +67,14 @@ function some(arr, iter) { }, Q()); } -// Map an array using an async (promised) iterator -function map(arr, iter) { +/** + Map an array using an async (promised) iterator + + @param {Array|List} arr + @param {Function(element, index)} + @return {Promise<List>} +*/ +function mapAsList(arr, iter) { return reduce(arr, function(prev, entry, i) { return Q(iter(entry, i)) .then(function(out) { @@ -43,18 +84,57 @@ function map(arr, iter) { }, []); } -// Wrap a fucntion in a promise +/** + Map an array or map + + @param {Array|List|Map|OrderedMap} arr + @param {Function(element, key)} + @return {Promise<List|Map|OrderedMap>} +*/ +function map(arr, iter) { + if (Immutable.Map.isMap(arr)) { + var type = 'Map'; + if (Immutable.OrderedMap.isOrderedMap(arr)) { + type = 'OrderedMap'; + } + + return mapAsList(arr, function(value, key) { + return Q(iter(value, key)) + .then(function(result) { + return [key, result]; + }); + }) + .then(function(result) { + return Immutable[type](result); + }); + } else { + return mapAsList(arr, iter) + .then(function(result) { + return Immutable.List(result); + }); + } +} + + +/** + Wrap a function in a promise + + @param {Function} func + @return {Funciton} +*/ function wrap(func) { - return _.wrap(func, function(_func) { - var args = Array.prototype.slice.call(arguments, 1); + return function() { + var args = Array.prototype.slice.call(arguments, 0); + return Q() .then(function() { - return _func.apply(null, args); + return func.apply(null, args); }); - }); + }; } module.exports = Q; +module.exports.forEach = forEach; module.exports.reduce = reduce; module.exports.map = map; module.exports.serie = serie; |