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 need to be with a timeout since if there is a network issue there is no point in retrying immediately.
Here is what I have so far:
function downloadItemWithRetryAndTimeout(url, retry, failedReason) {
    return new Promise(function(resolve, reject) {
        try {
            if (retry < 0 && failedReason != null) reject(failedReason);
            downloadItem(url);
            resolve();
        } catch (e) {
            setTimeout(function() {
                downloadItemWithRetryAndTimeout(url, retry - 1, e);
            }, 1000);
        }
    });
}
Obviously this will fail since the second (and on) time I call downloadItemWithRetryAndTimeout I don't return a promise as required.
How do I make it work correctly with that second promise?
P.S. incase it matters the code is running in NodeJS.
 
     
     
     
    