Praktikum_mf

Fetch с подсчётом попыток

Mar 30th, 2021
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fetchWithRetry(url, options = {}) {
  2.   const {tries = 1} = options;
  3.  
  4.     function onError(err){
  5.         const triesLeft = tries - 1;
  6.         if (!triesLeft){
  7.             throw err;
  8.         }
  9.  
  10.         return fetchWithRetry(url, {...options, tries: triesLeft});
  11.     }
  12.  
  13.     return fetch(url, options).catch(onError);
  14. }
Advertisement
Add Comment
Please, Sign In to add comment