Skip to content

Instantly share code, notes, and snippets.

@cevek
Created May 30, 2018 13:19
Show Gist options
  • Save cevek/aa9db0aa8a2829e30fef0f9d5e2fbeab to your computer and use it in GitHub Desktop.
Save cevek/aa9db0aa8a2829e30fef0f9d5e2fbeab to your computer and use it in GitHub Desktop.
Fibers with async await
var prev = Date.now();
var callbacks;
function wait() {
return new Promise((resolve) => {
if (Date.now() > prev + 8) {
if (callbacks === undefined) {
callbacks = [];
callbacks.push(resolve);
requestAnimationFrame(() => {
prev = Date.now();
callbacks.forEach(fn => fn());
callbacks = undefined;
});
} else {
callbacks.push(resolve);
}
return;
}
resolve();
});
}
async function work() {
await wait();
return 123;
}
async function main() {
for (let i = 0; i < 1e8; i++) {
await work()
}
}
main().catch(console.error);
setInterval(() => { console.log(123); }, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment