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

Time:03-06

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

CodePudding user response:

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

}
  • Related