• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Understanding the JavaScript Event Loop

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
If you've ever wondered how JavaScript handles asynchronous tasks like setTimeout, fetch, or Promises while being a single-threaded language—you're not alone.

? How JavaScript Executes Code


JavaScript runs in the browser using a JavaScript engine. When your code executes, it goes into the call stack, which handles synchronous tasks—code that runs line by line.

But when there's asynchronous code, things work a bit differently.

? Web APIs


The browser provides Web APIs to handle asynchronous operations.

When such code runs, it's sent off to the Web API environment provided by the browser—not the call stack. Once the asynchronous operation is complete, its callback is moved to the callback queue (or task queue).

? Microtasks vs Macrotasks


Not all tasks in the callback queue are treated equally.

  • Microtasks: Promises, queueMicrotask, MutationObserver
  • Macrotasks: setTimeout, setInterval, setImmediate, UI rendering

Microtasks have higher priority and are always executed before macrotasks in each cycle of the event loop.

? Event Loop


The event loop is the behind-the-scenes mechanism that keeps everything running smoothly.

It constantly checks: Is the call stack empty?

If yes, it picks the first task from the callback queue (starting with microtasks) and moves it into the call stack for execution.

This is how JavaScript handles asynchronous code without blocking the main thread.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу