0

I have a problem where the JavaScript array is stuck in pending. Is this because there are too many requests?

const removeBots = async(arr) => {
  try {
    for (let i = 0; i < arr.length; i++) {
      console.log(i)
      axios.get(`https://api.etherscan.io/api?module=account&action=txlist&address=${arr[i]}&apikey=${apiKey}`)
        .then(res => console.log(res.data.result.length))
    }
    console.log("Done");
    console.log(walletsFinal);
  } catch (e) {
    console.log("error", e)
  }
}

Edit- I noticed the formatting problem with the backticks and fixed it. I'm trying to get around the issue where I'm getting rate limited of 5 calls per second for the API.

EKattalog
  • 1
  • 1
  • What do you mean by "stuck in pending"? Do you mean the AJAX calls? – Barmar Jul 21 '21 at 19:22
  • What does "array is stuck in pending" mean? – Scott Hunter Jul 21 '21 at 19:22
  • Since you haven't used `await` when calling `axios.get()`, your loop should finish immediately without waiting for the AJAX requests to complete. – Barmar Jul 21 '21 at 19:24
  • @Barmar there's no await, but OP used .then syntax, so that shouldn't be the problem. – Voodu Jul 21 '21 at 19:50
  • @EKattalog that's not answer to your question, but you have wrong quotes in `axios.get` line. If you want to use template strings, you should use backticks (`) instead of current single quotes. Maybe that causes some problems with the request – Voodu Jul 21 '21 at 19:53
  • 1
    @Voodu Using `.then()` makes the `console.log()` wait for the response, but it doesn't make the loop wait. – Barmar Jul 21 '21 at 19:58
  • @Barmar Right, that's true. – Voodu Jul 21 '21 at 20:09
  • never heard about an array being stuck. well, there's a first for everything – Mihai T Jul 22 '21 at 18:13

1 Answers1

0

If your problem is that you're being ratelimited to 5 api calls/second, then just wait 200ms (250ms to be safe) between each call.

// https://stackoverflow.com/a/39914235/12101554
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

const removeBots = async(arr) => {
  try {
    for (let i = 0; i < arr.length; i++) {
      console.log(i)
      const res = await axios.get(`https://api.etherscan.io/api?module=account&action=txlist&address=${arr[i]}&apikey=${apiKey}`);
      console.log(res.data.result.length);
      await sleep(250);
    }
    console.log("Done");
    console.log(walletsFinal);
  } catch (e) {
    console.log("error", e)
  }
}
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34