Batch requests using web3.js
web3.js supports batch requests, which allows for the queuing up of multiple requests and processing them all at once. Batch requests are not exponentially faster. They are mainly used to ensure serial processing of requests, and sometimes can be faster as requests are processed asynchronously.
In this recipe, you will learn to create and process batch requests using web3.js.
Getting ready
You need to have web3.js installed in your application to use these methods. Ensure that you have a geth- or Ganache-based Ethereum network to connect and test these methods.
How to do it...
Here are the steps that must be followed to batch requests using web3.js:
- Creating and executing batch requests is different between versions of web3.js. If you are using web3.js v0.2x.x, use the following method to create a batch:
var batch =web3.createBatch();
- Now, add as many requests as you want to the queue:
var balance = web3.eth.getBalance.request( web3.eth.accounts[1], 'latest', ...