Created
March 20, 2024 21:54
-
-
Save lappi-lynx/808f99d46ffc00c980368fac7137d1b7 to your computer and use it in GitHub Desktop.
js/node async nuances
This file contains 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
const { promisify } = require('util'); | |
const sleep = promisify(setTimeout); | |
async function bar(n, s, t) { | |
setImmediate(() => process.stdout.write(s)); | |
await sleep(n); | |
return t; | |
} | |
async function foo() { | |
process.stdout.write('L'); | |
for (const m of await Promise.all([bar(20, 'N', 'R'), bar(10, 'T', 'E')])) | |
process.stdout.write(m) | |
} | |
sleep(50).then(() => process.stdout.write('A')); | |
new Promise((res) => { | |
process.stdout.write('H'); | |
res('O'); | |
}).then((m) => process.stdout.write(m)) | |
.finally(() => process.stdout.write('M')); | |
queueMicrotask(() => process.stdout.write(' ')); | |
process.nextTick(() => process.stdout.write('L')); | |
setTimeout(() => process.stdout.write('L'), 100); | |
setImmediate(() => process.stdout.write('O')); | |
process.stdout.write('E'); | |
foo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment