Skip to content

Instantly share code, notes, and snippets.

@jblashill
Last active May 5, 2023 08:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jblashill/9118700 to your computer and use it in GitHub Desktop.
Save jblashill/9118700 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
var Promise = require('es6-promise').Promise;
// idea borrowed from Q.spread
Promise.prototype.spread = function(fn) {
return this.then(function() {
if (!arguments || arguments.length === 0) {
return fn.apply();
}
return fn.apply(null, arguments[0]);
});
};
Array.prototype.promise = function() {
return Promise.all(this);
};
promptUser([query, newPassword])
.then(function(input) {
return [searchForUser(input.query), input.newPassword].promise();
})
.spread(function(result, newPassword) {
return updatePassword(result.id, newPassword);
})
.then(function() {
console.log("Password updated!");
})
.catch(handeError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment