Created
April 23, 2021 14:25
-
-
Save wheresjames/243826b727d760e3847cb0087e8e81f8 to your computer and use it in GitHub Desktop.
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 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