This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Is batch.execute() asynchronous? #3411
Labels
1.x
1.0 related issues
Comments
@ilyador You can manage the promise yourself, for example async function getLatestBlocks (n) {
const latest = await eth.getBlockNumber()
const blockNumbers = getBlockNumbers(latest, n)
const batch = new eth.BatchRequest()
const total = blockNumbers.length;
let counter = 0;
let blocks = [];
await new Promise(function(resolve, reject){
blockNumbers.forEach(blockNumber => {
batch.add(
eth.getBlock.request(blockNumber, (error, data) => {
if (error) return reject(error);
counter++;
blocks.push(data);
if (counter === total) resolve();
})
)
});
batch.execute()
});
return blocks
} ^^^ Haven't actually tried the above, may have syntax error or other problems |
Got it, thanx |
@ilyador I am actually trying to implement this exact thing, when I give a callback function with the batch request add, I am getting undefined for the callback for some strange reason. Would you mine sharing how you were able to get each block from multiple batch requests? |
For anyone wondering how to make the batch.execute asynchronous and get an Array of responses instead of calling requests callbacks individually, here's a function :
To use it :
|
I am getting following error |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I cannot figure out from the docs how to correctly use the data returned from the batch requests.
At first, I tried a naive implementation
But
blocks
was returning empty.I tried using
execute()
as a promise, but it is not (I gotexecute has no method "then"
).Since I am using React, I tried to populate the state in the
request
callback, but in that case, I have no indication of when the batch call is actually finished.So how do I know when
batch.execute()
has finished executing all the callbacks?The text was updated successfully, but these errors were encountered: