-
-
Save iolufemi/37750ab94ab5f6ee7325147e7d5799d6 to your computer and use it in GitHub Desktop.
An extension to the Promise prototype to "spread" resolved data from Promise.all() to multiple function parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.prototype.spread = function(fn) { | |
return this.then(function() { | |
if (!arguments || arguments.length === 0) { | |
return Promise.resolve(fn.apply()); | |
} | |
if(typeof arguments[0] === 'object' && arguments[0].length > 0){ | |
return Promise.all(arguments[0]); | |
}else{ | |
return Promise.resolve(arguments[0]); | |
} | |
}) | |
.then(function(args){ | |
if(args && !args.length){ | |
args = [args]; | |
} | |
return fn.apply(null, args); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment