Last active
April 25, 2023 16:03
-
-
Save xavierlepretre/0e24b1dc300e5aaea20c87e3d3039d29 to your computer and use it in GitHub Desktop.
Like `Promise.all()` but where all promises are started sequentially.
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
const Promise = require("bluebird"); | |
/** | |
* @param {!Array.<function.Promise.<Any>>} promiseArray. | |
* @returns {!Promise.<Array.<Any>>} The results of the promises passed to the function. | |
*/ | |
module.exports = function sequentialPromise(promiseArray) { | |
const result = promiseArray.reduce( | |
(reduced, promise, index) => { | |
reduced.results.push(undefined); | |
return { | |
chain: reduced.chain | |
.then(() => promise()) | |
.then(result => reduced.results[ index ] = result), | |
results: reduced.results | |
}; | |
}, | |
{ | |
chain: Promise.resolve(), | |
results: [] | |
}); | |
return result.chain.then(() => result.results); | |
}; |
I tried this out and getting this error message on line 13:
TypeError: promise is not a function
You did:
sequentialPromise([
Promise.resolve(1),
Promise.resolve(2)
])
What you have to do is:
sequentialPromise([
() => Promise.resolve(1),
() => Promise.resolve(2)
])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also use Web3 v1.0's
BatchRequest
: http://web3js.readthedocs.io/en/1.0/web3-eth.html#batchrequest