const fetch_retry = async (url, options, n) => { let error; for (let i = 0; i < n; i++) { try { response = await fetch(url, options); if (response === 200) { return response; }else{ throw new Error(response) } } catch (err) { error = err; //1. if (i + 1 === n) throw err; } } throw error; }; let response = fetch_retry("/someUrl/json", options, 10);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter