Skip to content

Commit 2a6bd37

Browse files
author
Nick Santos
committed
add spread() to kew
1 parent e98d575 commit 2a6bd37

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

kew.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,36 @@ Promise.prototype.failBound = function (onFail, scope, var_args) {
298298
return promise
299299
}
300300

301+
/**
302+
* Spread a promises outputs to the functions arguments.
303+
* @param {?function(this:void, ...): RESULT|undefined} onSuccess
304+
* @return {!Promise.<RESULT>} returns a new promise with the output of the onSuccess or
305+
* onFail handler
306+
* @template RESULT
307+
*/
308+
Promise.prototype.spread = function (onSuccess) {
309+
return this.then(allInternal)
310+
.then(function (array) {
311+
return onSuccess.apply(null, array)
312+
})
313+
}
314+
315+
/**
316+
* Spread a promises outputs to the functions arguments.
317+
* @param {function(this:SCOPE, ...): RESULT} onSuccess
318+
* @param {SCOPE} scope Object whose context callback will be executed in.
319+
* @param {...*} var_args Additional arguments to be passed to the promise callback.
320+
* @return {!Promise.<RESULT>} returns a new promise with the output of the onSuccess
321+
* @template SCOPE, RESULT
322+
*/
323+
Promise.prototype.spreadBound = function (onSuccess, scope, var_args) {
324+
var args = Array.prototype.slice.call(arguments, 2)
325+
return this.then(allInternal)
326+
.then(function (array) {
327+
return onSuccess.apply(scope, args.concat(array))
328+
})
329+
}
330+
301331
/**
302332
* Provide a callback to be called whenever this promise is either resolved
303333
* or rejected.
@@ -604,6 +634,16 @@ function all(promises) {
604634
if (arguments.length != 1 || !Array.isArray(promises)) {
605635
promises = Array.prototype.slice.call(arguments, 0)
606636
}
637+
return allInternal(promises)
638+
}
639+
640+
/**
641+
* A version of all() that does not accept var_args
642+
*
643+
* @param {!Array.<!Promise>} promises
644+
* @return {!Promise.<!Array>}
645+
*/
646+
function allInternal(promises) {
607647
if (!promises.length) return resolve([])
608648

609649
var outputs = []
@@ -676,6 +716,15 @@ function allSettled(promises) {
676716
return promise
677717
}
678718

719+
/**
720+
* Takes an array of results and spreads them to the arguments of a function.
721+
* @param {!Array} array
722+
* @param {!Function} fn
723+
*/
724+
function spread(array, fn) {
725+
resolve(array).spread(fn)
726+
}
727+
679728
/**
680729
* Create a new Promise which looks like a deferred
681730
*
@@ -788,6 +837,7 @@ module.exports = {
788837
, nfcall: nfcall
789838
, resolve: resolve
790839
, reject: reject
840+
, spread: spread
791841
, stats: stats
792842
, allSettled: allSettled
793843
, Promise: Promise

test/spread.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var Q = require('../kew')
2+
3+
exports.testSpreadStatic = function (test) {
4+
Q.spread([Q.resolve('a'), 'b'], function (a, b) {
5+
test.equal('a', a)
6+
test.equal('b', b)
7+
test.done()
8+
})
9+
}
10+
11+
exports.testSpreadMethod = function (test) {
12+
Q.resolve(true)
13+
.then(function () {
14+
return ['a', 'b']
15+
})
16+
.spread(function (a, b) {
17+
test.equal('a', a)
18+
test.equal('b', b)
19+
test.done()
20+
})
21+
}
22+
23+
exports.testSpreadBoundMethod = function (test) {
24+
Q.resolve(true)
25+
.then(function () {
26+
return [Q.resolve('a'), 'b']
27+
})
28+
.spreadBound(function (c, a, b) {
29+
test.equal('scope', this.scope)
30+
test.equal('c', c)
31+
test.equal('a', a)
32+
test.equal('b', b)
33+
test.done()
34+
}, {scope: 'scope'}, 'c')
35+
}

0 commit comments

Comments
 (0)