Skip to content

Instantly share code, notes, and snippets.

@johnrhampton
Created September 9, 2016 19:46
Show Gist options
  • Save johnrhampton/50c726112a60feb6e925cca4dbadbaec to your computer and use it in GitHub Desktop.
Save johnrhampton/50c726112a60feb6e925cca4dbadbaec to your computer and use it in GitHub Desktop.
recursive function
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