How can I keep fetching the webpage while it didn’t get response being in a loop?
for (el of NodeList) {
const url = el.getAttribute('href')
const res = await fetch(url) //if no response, below code won't execute
const html = await response.text()
const parser = new DOMParser()
const doc = parser.parseFromString(html, 'text/html')
alert('parsed successfully')
}
So as it works in a loop and sends very many requests at a time, even a small and short issue with network can spoil the whole process, what can I do?
2
Answers
Don’t loop
Try something like this (not tested)
I guess you should handle network errors. Try to do this with retry mechanism that tries to fetch the webpage again if no response is received.
You can make function fetchWithRetry that takes two arguments: the URL to fetch and the maximum number of retries (to avoid infinite loop).
Then you can modify your code to use the fetchWithRetry function instead of the fetch function.