Created
August 23, 2017 01:14
-
-
Save AaronHarris/e7c86f759effbe7086ec2200f2cdfe67 to your computer and use it in GitHub Desktop.
Make a promise retryable a certain number of times without balooning memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function retry(gen /*: Function<Promise> */, try = 2) { | |
| if (try < 1) return gen(); // will not retry | |
| return gen().catch(retry.bind(retry, gen, i - 1)) | |
| } | |
| // or | |
| function retry(gen/*: Function<Promise> */, try = 2) { | |
| function again(i) { | |
| if (i < 1) return gen(); | |
| return gen.catch(again.bind(this, i-1)); | |
| } | |
| return gen().catch(again.bind(this, try - 1)); | |
| } | |
| function retry(promiseFun) { | |
| function doPromise(promiseFun) { | |
| return | |
| } | |
| return promiseFun().catch(error => { | |
| retry | |
| }) | |
| maxRetries = 5; | |
| for(let i = 0; i < maxRetries; i++) { | |
| yield promiseFun() | |
| } | |
| } | |
| retry(Dao.deleteStuff).then(() => console.log('Successfully Updated Event')).catch('Max retries reached. Failed to clean PSIM_SITES'); | |
| Dao.deleteStuff().catch(retry); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment