Created
January 7, 2021 16:56
-
-
Save ccampanale/45fe8380e359df4dd48d13e4906f4a2a to your computer and use it in GitHub Desktop.
NodeJS Timers
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
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