Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
How to make API calls inside a for loop using the index as a parameter

I'm trying to build a very simple App using Web3 and JS. Basically the App should return the IDs of the tokens that a given user that is interacting with it holds in his wallet, and display the relative images. Here I'm interested in the first part. I don't think this is the most efficient way to do that (lot of calls), but still:

const getOwnerOf = (owner) => {
    let ownedTokensArray = [];
    for (let i = 1; i < 3333; i++) {
      let ownerAddress = blockchain.smartContract.methods.ownerOf(i).call();
      ownerAddress
      .then(function(result){
        console.log(result); // returns a wallet address (e.g 0x476e62b30E2587Ea937C19e2b60781e334fa29d7)
        if (String(result) === String(owner)) {
          ownedTokensArray.push(i);
        }
      })
    }
    console.log(ownedTokensArray); // should return an array of IDs but instead is []
  }

Once I've collected all the IDs in the ownedTokensArray, I would like to make another function with more or less the same logic, where I iterate over the IDs array and make a fetch to an API using the IDs in order to display the image data connected to the correspondent tokens.

The problem is that ownedTokensArray is [] and while I've more or less guessed the concepts behind asynchronous calls I'm still facing difficulties. Do I have to keep nesting the function by using .then()? Is there a more convenient way (both syntax-wise and logically) to do this? Why asynchronous calls are so difficult to understand thoroughly?

Thank you very much for your time.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!