How do you spread a lot of requests over time in nodejs?

428 views Asked by At

I'm working on a backend that has to request data from Moralis (web3), but how can I spread the requests in time so that I don't exceed the max requests each minute and so it doesn't time out. Right now I'm calling a function within a for loop.

const allContractInfo = await Moralis.Web3API.token.getAllTokenIds({ address: address, chain: "rinkeby", });
const nftItems = allContractInfo.result;

for(let i = 0; i < allContractInfo.total; i++){
  UpdateItemAuction(nftItems[i].token_address, nftItems[i].token_id)
}
1

There are 1 answers

0
kawadhiya21 On BEST ANSWER

This is just an example to fix this problem. Hope you can find better solutions.

const allContractInfo = await Moralis.Web3API.token.getAllTokenIds({ address: address, chain: "rinkeby", });
const nftItems = allContractInfo.result;

for(let i = 0; i < allContractInfo.total; i++){
  setTimeout(function() {
    UpdateItemAuction(nftItems[i].token_address, nftItems[i].token_id);
  }, i*1000);
}

This will ensure that requests are fired 1/sec.