Questions tagged [event-loop]

Event loop refers to an infinite cycle of actions which is used for processing data based on callbacks and messages.

An event loop watches for I/O and timer state changes. It queues functions while waiting for unavailable resources, then dispatches them when resources are available.

References

1046 questions
4
votes
1 answer

How to convert a cycle to not block a Tcl event loop?

I am writing a TCL/Tk application which at some point has a button that eventually runs the following loop: while {[llength $Queue]>0} { thread::mutex lock [tsv::set Workers Mutex] while {[tsv::llength Workers Available]==0} { …
4
votes
1 answer

process.nextTick vs microtask execution order

I have following code snippet in node js Promise.resolve().then(() => { Promise.resolve().then(() => console.log('promise')); process.nextTick(() => console.log('nextTick')); }); // promise // nextTick And I have this…
4
votes
1 answer

Why must MV3 Chrome extensions (using Service Workers) "register listeners in the first turn of the event loop"?

So I am in the process of migrating a MV2 extension which used persistent Background pages to MV3. In the Chrome migration guide [https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/#event_listeners] it says : In order for…
4
votes
3 answers

setTimeout callbacks have different execution orders in Firefox and Chrome

When I run this code in Firefox and Chrome, the results are different: function run() { setTimeout(() => console.log("1"), 0); setTimeout(() => console.log("2"), 100); let start = Date.now(); while (Date.now() - start < 200) { // do…
UnitTset
  • 259
  • 1
  • 5
4
votes
2 answers

Do events bubble in microtasks?

On click, this outputs 1 then 2: const myElem = document.getElementById("myElem") myElem.addEventListener('click', () => queueMicrotask(() => console.log(1))); window.addEventListener('click', () => console.log(2));
Alexey Berezkin
  • 1,513
  • 1
  • 10
  • 18
4
votes
0 answers

Calling async_to_sync in django sync view (WSGI) causes RuntimeError

I am running 2 kinds of django web server simultaneously in my architecture, gunicorn(WSGI with worker_class=gevent) and channels(ASGI) respectively. WSGI server is responsible for handling HTTP request and channels is responsible for websocket…
4
votes
0 answers

Node.js Event-Loop. Why callbacks from the check queue execute before those from the poll queue while Node.js DOCs state vice versa?

According to the Node.js DOCs, when the event-loop enters its poll phase and the poll queue is not empty, the callbacks in the poll queue should get executed before the event-loop proceeds further to its check phase. In reality, however, the…
Igor C.
  • 41
  • 2
4
votes
1 answer

Why does an async function finishes executing after a promise started later?

This is something about the event loop I don't understand. Here's the code: async function async1() { console.log('async1 start'); await async2(); console.log('async1 end'); } async function async2() { console.log('async2 start'); …
Li Wenchi
  • 45
  • 3
4
votes
2 answers

Is leaving useless synchronous code under Async/Await an anti-pattern?

To my understanding, the point behind await is to 'await' the acting upon the resolved value of a promise until it is encountered as a microtask, as Jake Archibald explains here. This video by LLJS shows that async-await is essentially syntactical…
Clever7-
  • 75
  • 1
  • 9
4
votes
1 answer

How does Node.js process incoming requests?

In order to dive into more complex concepts about Node.js, I am doing some research to make sure I understand the principles about the language and the basic building blocks it´s build upon. As far as I´m concerned, Node.js relies in the reactor…
Luis Orbaiceta
  • 443
  • 4
  • 15
4
votes
2 answers

How does javascript manage to catch exception in late .catch handler?

The other day I came across the following peace of code: let promise = Promise.reject(new Error('Promise Failed!')); // 1 setTimeout(() => promise.catch(err => alert('caught')), 1000); // 2 // 3 So I was quite surprised to find out the error was…
fekaloid
  • 195
  • 1
  • 2
  • 9
4
votes
1 answer

What does fetch do with event loop in browser?

I try to understand event loop of javascript, and I am confused with fetch. What I know console.log("1") Promise.resolve(2) .then(data => { console.log(data) }) console.log("3") The above code executes with results: 1, 3, 2. Since in the first…
Evian
  • 1,035
  • 1
  • 9
  • 22
4
votes
1 answer

Any example proving microtask is executed before rendering?

I saw several articles says that render steps are after microtasks. I test it with this code:

test