Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Last active December 15, 2024 10:53
Show Gist options
  • Save phatnguyenuit/52bc3219ed86538f96428116c54266dd to your computer and use it in GitHub Desktop.
Save phatnguyenuit/52bc3219ed86538f96428116c54266dd to your computer and use it in GitHub Desktop.
Interview Preparation for Angular, ReactJS
@phatnguyenuit
Copy link
Author

Event loop examples

// Start
// add to callstack and execute -> synchronous -> log 1
console.log(1);

// add to callstack and execute (done)
// -> asynchronous (WebAPI Timer)
// -> WebAPI count time
// -> Wait until time up
// -> And enqueue callback into Task Queue
setTimeout(() => console.log(2), 1000);

// add to callstack and execute (done)
// -> asynchronous
// -> enqueue callback into MicroTask Queue
Promise.resolve().then(() => console.log(3));
// same priority
queueMicrotask(() => console.log("3+"));

new Promise((resolve) => setTimeout(resolve, 3000)).then(() =>
  console.log("3*")
);

// add to callstack and execute (done)
// -> asynchronous (WebAPI Timer)
// -> WebAPI count time
// -> Wait until time up
// -> And enqueue callback into Task Queue
setTimeout(() => console.log(4), 500);

// add to callstack and execute
// -> synchronous
// log 5
console.log(5);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment