I need to implement the following logic.
I need to download an image, but sometime it happens that a downloaded file is corrupted, so I need to try to download it again. Here is how my download promise looks like.
return Downloader.downloadImage(downloadUrl, fileName).then((filename) => {
            // Update some info, save ... and return new promise
            return doStuffAndReturnPromise(filename);
        });
But as I've described above I need to verify if the downloaded file is valid image and only then return promise with success.
Here is a skeleton.
 return new Promise(function (resolve, reject) {
            let retryCounter = MyService.RETRY_COUNT;
            let success = false;
            while (retryCounter > 0 && !success) {
                 // Run new promise but wait until it completes
                 // Decrease the counter
                retryCounter--;
            }
        });
The problem here is that I need to run the download promise synchronously and only then continue iterating and running a new promise.
Please suggest the best way to solve this problem elegantly.
Thanks.
 
     
    