JS Fetch and retry with recursion

PHOTO EMBED

Thu Dec 17 2020 00:23:29 GMT+0000 (Coordinated Universal Time)

Saved by @DBZFYAM #javascript

const fetch_retry = async (url, options, n) => {
    try {
       response = await fetch(url, options);
       if (response === 200) {
         return response;
       }else{
         throw new Error(response)
       }
    } catch(err) {
      //.1      
      if (n === 1) throw err;
   
      //2.
      response = await fetch_retry(url, options, n - 1);
      if (response === 200) {
         return response;
      }else{
         throw new Error(response)
      }
    }
};
let response = fetch_retry("/someUrl/json", options, 10);
content_copyCOPY

https://medium.com/javascript-in-plain-english/modern-practical-javascript-code-snippets-4002a7a4ba9c