function retry(fn, retriesLeft = 5, interval = 1000) {
return new Promise((resolve, reject) => {
fn()
.then(resolve)
.catch(error => {
if (retriesLeft === 1) {
// reject('maximum retries exceeded');
reject(error);
return;
}
setTimeout(() => {
// Passing on "reject" is the important part
retry(fn, retriesLeft - 1, interval).then(resolve, reject);
}, interval);
});
});
}