Skip to content

Instantly share code, notes, and snippets.

@autioch
Forked from barcicki/test.js
Last active May 16, 2017 13:39
Show Gist options
  • Save autioch/f1cab644b143900730a5362b25254771 to your computer and use it in GitHub Desktop.
Save autioch/f1cab644b143900730a5362b25254771 to your computer and use it in GitHub Desktop.
Call order test
/* 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