@@ -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
0 commit comments