Skip to content

Instantly share code, notes, and snippets.

@ccampanale
Created January 7, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccampanale/45fe8380e359df4dd48d13e4906f4a2a to your computer and use it in GitHub Desktop.
Save ccampanale/45fe8380e359df4dd48d13e4906f4a2a to your computer and use it in GitHub Desktop.
NodeJS Timers
async function testImmediate() {
console.log('A');
setImmediate(() => {
console.log('B')
});
await new Promise((resolve) => {
console.log('C');
setTimeout(() => {
console.log('D');
resolve();
}, 0);
console.log('E');
});
console.log('F');
}
async function testImmediate2() {
console.log('A');
await new Promise((resolve) => {
console.log('C');
setTimeout(() => {
console.log('D');
resolve();
}, 0);
console.log('E');
});
setImmediate(() => {
console.log('B')
});
console.log('F');
}
testImmediate1().then(() => console.log('G')).catch(err => console.error(err));
// => A C E B D F G
testImmediate2().then(() => console.log('G')).catch(err => console.error(err));
// => A C E D F G B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment