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; } }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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) } }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0