Last active
May 5, 2023 08:43
-
-
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
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
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