Полагаю, makeRequest
- это описанная здесь функция , и если вам нужен классический JS, вы можете перевести ES6 как const x = (a,b=0)=>(c)=>22
в function x(a,b){if(b===undefined){b=0} return function(c){...
Ваша функция построения повторов может выглядеть как-токак это:
const createRetry = (
maxRetries, //how many times to try
passingResults = (id) => id, //function to "pass the result"
tries = 0, //how many times tried
) => (method, url) =>
makeRequest(method, url)
.then(passingResults)
.catch(
(error) =>
tries < maxRetries
? createRetry(//call itself again when it failed
maxRetries,
tries + 1,//add one to tries as we've just tried
passingResults,
)(method, url)
: Promise.reject(error),//tried max tries times, just reject
);
const retryTwiceAndNameIsInResponse = createRetry(
2, //retry twice
//function to check the xhr json result, if result has a name
// property then it passes and just returns the result,
// if not then it will reject the promise with an error
// a function can be optionally passed, if it's not passed
// then it will default to an id function (just returns what it gets)
(result) =>
result.name
? result
: Promise.reject(new Error('no name in result')),
//no need to pass tries in as that defaults to 0 and indicates how
// many times the method has tried to get the right result
);
//to use the function:
retryTwiceAndNameIsInResponse(method,url)
.then(response=>console.log('passed, response is:',response))
.catch(error=>console.log('we failed:',error))