-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is batch.execute() asynchronous? #3411
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 |
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: