Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Last active November 18, 2020 22:06
Show Gist options
  • Save kriskowal/e692ab16207f5f09e0095000d7142a59 to your computer and use it in GitHub Desktop.
Save kriskowal/e692ab16207f5f09e0095000d7142a59 to your computer and use it in GitHub Desktop.
async function avery() {
await null;
for (let i = 0; i < 10; i += 1) {
console.log('await');
await null;
}
}
function blake() {
return new Promise((resolve) => {
let i = 10;
function tick() {
if (i < 0) {
return resolve();
}
i -= 1;
console.log('setImmediate');
setImmediate(tick);
}
setImmediate(tick);
});
}
function clarke() {
return new Promise((resolve) => {
let i = 10;
function tick() {
if (i < 0) {
return resolve();
}
i -= 1;
console.log('nextTick');
process.nextTick(tick);
}
process.nextTick(tick);
});
}
function danny() {
return new Promise((resolve) => {
let i = 10;
function tick() {
if (i < 0) {
return resolve();
}
i -= 1;
console.log('setTimeout');
setTimeout(tick, 0);
}
setTimeout(tick, 0);
});
}
async function main() {
return Promise.all([avery(), blake(), clarke(), danny()]);
}
main();
nextTick
nextTick
nextTick
nextTick
nextTick
nextTick
nextTick
nextTick
nextTick
nextTick
nextTick
await
await
await
await
await
await
await
await
await
await
setTimeout
setImmediate
setTimeout
setImmediate
setImmediate
setImmediate
setImmediate
setImmediate
setImmediate
setImmediate
setImmediate
setImmediate
setImmediate
setTimeout
setTimeout
setTimeout
setTimeout
setTimeout
setTimeout
setTimeout
setTimeout
setTimeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment