Created
September 9, 2016 19:46
-
-
Save johnrhampton/50c726112a60feb6e925cca4dbadbaec to your computer and use it in GitHub Desktop.
recursive function
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 doSomethingForAWhile() { | |
return new Promise((resolve, reject) => { | |
var maxStatusChecks = 10; | |
var statusCheckCount = 0; | |
var delay = 750; | |
(function doSomething() { | |
return checkSomethingAsync() | |
.then(() => { | |
return timeout(delay || 500); | |
}) | |
.then(() => { | |
return shouldRecurse() ? doSomething() : true; | |
}) | |
}()) | |
.then(() => { | |
return fetch(url); | |
}) | |
.then(response => { | |
return resolve(response); | |
}) | |
.catch(err => { | |
return reject(err); | |
}); | |
function timeout(time) { | |
return new Promise(resolve => { | |
if (!shouldRecurse()) { | |
return resolve(); | |
} | |
setTimeout(resolve, time); | |
}); | |
} | |
function shouldRecurse() { | |
return statusCheckCount < maxStatusChecks; | |
} | |
}); | |
} | |
function checkSomethingAsync() { | |
return new Promise((resolve, reject) => { | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment