-
-
Save autioch/f1cab644b143900730a5362b25254771 to your computer and use it in GitHub Desktop.
Call order test
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
/* What will be the console order. */ | |
function busy(duration) { | |
const current = Date.now(); | |
let future = Date.now(); | |
while (future - current < duration) { | |
future = Date.now(); | |
} | |
} | |
function test(name, depths = 1) { | |
console.log(name); | |
if (depths > 0) { | |
requestAnimationFrame(() => test(`${name} > requestAnimationFrame`, depths - 1)); | |
setImmediate(() => test(`${name} > setImmediate`, depths - 1)); | |
setTimeout(() => test(`${name} > setTimeout 100`, depths - 1), 100); | |
setTimeout(() => test(`${name} > setTimeout 0`, depths - 1)); | |
process.nextTick(() => test(`${name} > nextTick`, depths - 1)); | |
Promise.resolve().then(() => test(`${name} > resolve 1`, depths - 1)).then(() => test(`${name} > resolve 2`, depths - 1)); | |
Promise.resolve().then(() => test(`${name} > resolve 3`, depths - 1)); | |
busy(1000); | |
} | |
} | |
test('Start', 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment