I have a function downloadItem that may fail for network reasons, I want to be able to retry it a few times before actually rejecting that item. The retries nee
function downloadItemWithRetryAndTimeout(url, retry) {
return new Promise(function(resolve, reject) {
var tryDownload = function(attempts) {
try {
downloadItem(url);
resolve();
} catch (e) {
if (attempts == 0) {
reject(e);
} else {
setTimeout(function() {
tryDownload(attempts - 1);
}, 1000);
}
}
};
tryDownload(retry);
});
}