diff options
Diffstat (limited to 'lib/utils/promise.js')
-rw-r--r-- | lib/utils/promise.js | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/lib/utils/promise.js b/lib/utils/promise.js index b0cfb34..b8ab63c 100644 --- a/lib/utils/promise.js +++ b/lib/utils/promise.js @@ -9,7 +9,7 @@ var Immutable = require('immutable'); @return {Promise<Mixed>} */ function reduce(arr, iter, base) { - arr = Immutable.List(arr); + arr = Immutable.Iterable.isIterable(arr)? arr : Immutable.List(arr); return arr.reduce(function(prev, elem, i) { return prev.then(function(val) { @@ -74,7 +74,7 @@ function some(arr, iter) { @param {Function(element, index)} @return {Promise<List>} */ -function map(arr, iter) { +function mapAsList(arr, iter) { return reduce(arr, function(prev, entry, i) { return Q(iter(entry, i)) .then(function(out) { @@ -85,6 +85,38 @@ function map(arr, iter) { } /** + 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 fucntion in a promise @param {Function} func |