I want to. retry my js dynamic import if it failed. I found 1 solution here - How to manually load webpack async chunks, when any dynamic import fails to load file?
function retry(fn, retriesLeft = 5, interval = 1000) {
return new Promise((resolve, reject) => {
fn()
.then(resolve)
.catch((error) => {
setTimeout(() => {
if (retriesLeft === 1) {
// reject('maximum retries exceeded');
reject(error);
return;
}
// Passing on "reject" is the important part
//HOW DOES THIS PART OF CODE WORKS? WHY WE NEED THEN CALLBACK WITH resolve, reject callbacks?
retry(fn, retriesLeft - 1, interval).then(resolve, reject);
}, interval);
});
});
}
(we called retry function which return new promise) HOW DOES THIS PART OF CODE WORKS? WHY WE NEED ".then" CALLBACK WITH resolve, reject callbacks?
why we need
.then(resolve, reject);
this construction?
Is it really necessary to update the state (rejected) of the promise to work from outside?