diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-09-16 23:39:44 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-09-16 23:39:44 +0100 |
| commit | 973bc5820a702ebedbaeb4ceecfea64f148dbafb (patch) | |
| tree | 81970e4e99c1d3abd1c6997808b1e1b2672a179a /lib | |
| parent | 5aa68a22458cdc77443d44d6b923e61de0fdac8d (diff) | |
Implement props in promises module
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/promises.js | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/promises.js b/lib/promises.js index b292331..6ceee1c 100644 --- a/lib/promises.js +++ b/lib/promises.js @@ -3,10 +3,38 @@ var bluebird = require("bluebird/js/release/promise")(); exports.defer = defer; exports.resolve = bluebird.resolve; -exports.props = bluebird.props; exports.reject = bluebird.reject; exports.mapSeries = bluebird.mapSeries; +var props = exports.props = function(obj) { + // We rely on .keys() and .values() returning properties in the same order. + var keys = Object.keys(obj); + var values = Object.values(obj); + + var result = bluebird.all(values).then(function(resolvedValues) { + var resolvedObj = {}; + for (var index = 0; index < keys.length; index++) { + resolvedObj[keys[index]] = resolvedValues[index]; + } + return resolvedObj; + }); + + return addAlsoToPromise(result); +}; + +function addAlsoToPromise(promise) { + promise.also = function(func) { + var result = this.then(function(value) { + var returnValue = _.extend({}, value, func(value)); + return props(returnValue); + }); + + return addAlsoToPromise(result); + }; + + return promise; +} + exports.try = function(func) { try { return bluebird.resolve(func()); |
