Created
May 30, 2018 13:19
-
-
Save cevek/aa9db0aa8a2829e30fef0f9d5e2fbeab to your computer and use it in GitHub Desktop.
Fibers with async await
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
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