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

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)
}


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kawadhiya21