Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to bypass a value that does not exist in an asynchronous data fetch loop?

I wrote a script that will invoke a function on a blockchain that returns a value. I have to collect these values from 0 to 5999. Unfortunately, several dozen values do not exist, and the loop encountering them stops the operation. How to prevent it?

let uriArray = [];
   for (let tokenId = 0; tokenId < 5999; tokenId++) {
    let result = await Gateway.tokenURI(tokenId);
     console.log(result);
     uriArray.push(result);

     if (result = '') {
       continue;
     }
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try using a try catch, it is stopping I think every time there is a request error.

let uriArray = [];

for (let tokenId = 0; tokenId < 5999; tokenId++) {
  try{
      let result = await Gateway.tokenURI(tokenId);
      console.log(result);
      uriArray.push(result);
  }catch(e){
    console.log(e)
  }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading