Last active
May 25, 2019 10:56
-
-
Save xavierlepretre/516cf53a8e1fe0ddb85fb94c5ca0d7ad to your computer and use it in GitHub Desktop.
Like `Promise.all()` but where all promises are started sequentially and it takes an object.
This file contains 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
const Promise = require("bluebird"); | |
/** | |
* @param {!Object.<function.<Promise.<Any>>>} promiseObject. Each key maps to a function | |
* that returns a promise. | |
* @returns {!Promise.<Object.<Any>>} The results of the promises passed to the function. | |
*/ | |
module.exports = function sequentialPromiseNamed(promiseObject) { | |
const result = Object.keys(promiseObject).reduce( | |
(reduced, key) => { | |
return { | |
chain: reduced.chain | |
.then(() => promiseObject[ key ]()) | |
.then(result => reduced.results[ key ] = result), | |
results: reduced.results | |
}; | |
}, | |
{ | |
chain: Promise.resolve(), | |
results: {} | |
}); | |
return result.chain.then(() => result.results); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Prints
See full tests: https://github.com/b9lab/promise-extensions/blob/master/test/allSequential.js