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
13
votes
3 answers

How many events can Node.js queue?

From what I see, if an event in Node take a "long time" to be dispatched, Node creates some kind of "queue of events", and they are triggered as soon as possible, one by one. How long can this queue be?
carduh
  • 529
  • 1
  • 4
  • 13
13
votes
2 answers

Why is an event loop needed for Asynchronous I/O

I've done lots of development in C#/.Net, and the asynchronous story has always been there from day one (admittedly the API's have changed significantly over the years from begin/end to events, to Task with async/await). For the last year or so…
BFree
  • 102,548
  • 21
  • 159
  • 201
13
votes
2 answers

Single thread concept of JavaScript running in browser

The following figure is taken from Chapter 3 of the book Secrets of the JavaScript Ninja by Jon Resig. Here the author is explaining the browser event loop. The book has to say this : It’s important to note that the browser mechanism that puts the…
Geek
  • 26,489
  • 43
  • 149
  • 227
13
votes
4 answers

Run NodeJS event loop / wait for child process to finish

I first tried a general description of the problem, then some more detail why the usual approaches don't work. If you would like to read these abstracted explanations go on. In the end I explain the greater problem and the specific application, so…
Joachim Kurz
  • 2,875
  • 6
  • 23
  • 43
12
votes
3 answers

JavaScript async callbacks - Promise and setTimeout

In the following code: setTimeout(() => console.log("hello"), 0); Promise.resolve('Success!') .then(console.log) What should happen in my understanding: setTimeout is called => print hello directly added to callback queue as time is…
Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
12
votes
3 answers

Nodejs - What is maximum thread that can run same time as thread pool size is four?

Requirement is concurrent request 1000 per second and IO operation such as database queries on each request. As nodejs works on event loop and it will assign the IO operation to thread pool, but thread pool default size is 4, so at same time maximum…
Bhavin
  • 179
  • 1
  • 2
  • 13
12
votes
1 answer

Questions about the order of execution of this code snippet

So I been reading a tutorial about Javascript promises these days. here is an example from it used to explain the macrotask queue(i.e. the event loop) and the microtask queue. let promise = Promise.reject(new Error("Promise…
Joji
  • 4,703
  • 7
  • 41
  • 86
12
votes
3 answers

Poll phase in NodeJS event loop

I was going through node docs for event loop and I got very confused. It says - timers: this phase executes callbacks scheduled by setTimeout() and setInterval(). I/O callbacks: executes almost all callbacks with the exception of close callbacks,…
Krrish Raj
  • 1,505
  • 12
  • 28
12
votes
1 answer

Should I use two asyncio event loops in one program?

I want use the Python 3 asyncio module to create a server application. I use a main event loop to listen to the network, and when new data is received it will do some compute and send the result to the client. Does 'do some compute' need a new event…
wenhui su
  • 131
  • 1
  • 1
  • 3
12
votes
4 answers

What function gets put into EventLoop in NodeJs and JS

I've been reading some NodeJs articles in order to understand its async nature during which I found this and really liked it Node.js, Doctor’s Offices and Fast Food Restaurants – Understanding Event-driven Programming There is a thing called…
Tarik
  • 79,711
  • 83
  • 236
  • 349
12
votes
2 answers

Single threaded and Event Loop in Node.js

First of all, I am starter trying to understand what is Node.Js. I have two questions. First Question From the article of Felix, it said "there can only be one callback firing at the same time. Until that callback has finished executing, all other…
Nay Lin Aung
  • 725
  • 1
  • 9
  • 21
11
votes
2 answers

javascript event-loop question

I wonder how the event-loop works in javascript, I am using node.js but I guess that the same question apply to browsers. I have some async call (let's say setTimeout or $.ajax or fs.readFile) and after a while the event-loop executes the…
user815070
11
votes
2 answers

How to queue a microtask if the browser doesn't support native Promises?

It's better to write code that doesn't rely on the timing of immediate callbacks (like microtasks vs macrotasks), but let's put that aside for the moment. setTimeout queues a macrotask, which, at a minimum, waits to start until all microtasks (and…
Snow
  • 3,820
  • 3
  • 13
  • 39
11
votes
1 answer

Does JavaScript spawn threads for non-blocking AJAX?

The general perception is that JavaScript is intrinsically single-threaded but it can run asynchronously. I wonder how a single-threaded model like this handles AJAX requests that are non-blocking? Lets say a non-blocking AJAX request is fired in a…
Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100
11
votes
2 answers

Assign priority to nodejs tasks in a event loop

Is there any way by which one can apply priority to Node.js task's in a event loop. I want to assign priority to task which are present in a event loop of nodejs. Suppose in a event loop there are 5 jobs A,B,C,D,E which having same priority and then…
Sanket
  • 945
  • 11
  • 24