Skip to content

Instantly share code, notes, and snippets.

@wheresjames
Created April 23, 2021 14:25
Show Gist options
  • Save wheresjames/243826b727d760e3847cb0087e8e81f8 to your computer and use it in GitHub Desktop.
Save wheresjames/243826b727d760e3847cb0087e8e81f8 to your computer and use it in GitHub Desktop.
function promiseWhile(cond, prom) {
return prom().then(r=> { return cond(r) ? promiseWhile(cond, prom) : r; });
}
function countTo(n) {
let i = 0;
return promiseWhile((r)=> r < n, () => {
return new Promise((resolve, reject)=> {
console.log(i);
setTimeout(()=>{i++; resolve(i);}, 1000);
});
});
}
countTo(11).then(r=> { console.log(`Counted to ${r}`); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment