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 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); | |
}; |
Because of the nature of Web3 and Javascript, it may happen that launching an aggressive Promise.all[ promise1, promise2 ]
does not act as expected. What may have happened is that the non-atomic requestId++
that takes place before each RPC call was not, well, atomic.
This "Sequential Promises" gist makes sure that all promises of the array are executed in sequence. This incurs an obvious time penalty.
You can also use Web3 v1.0's BatchRequest
: http://web3js.readthedocs.io/en/1.0/web3-eth.html#batchrequest
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
Example:
Prints
See full tests: https://github.com/b9lab/promise-extensions/blob/master/test/allSequential.js