0

So I kind of understand the JS event loop, but still have a few questions. Here is my scenario and a few questions.

So let's say I have these functions:

  1. function1 - reads an absolute huge file.
  2. function2 - console.log("Hey");
  3. function3 - console.log("What's up");

The way I am understanding this, and correct me if I'm wrong, what would happen is that the function1, function2, and function3, would be added to the queue. Then function1 would be added to the call stack followed by the next two functions.

Now the part where I'm confused is because the first function is going to take an extremely long time what happens to it? Does it get pushed somewhere else so that the next two functions are executed? I think the answer to this is that the only way it gets pushed somewhere else so that you can continue running is to make it an asynchronous function. And the way you make it a asynchronous function is either by using a callback function or promises. If this is the case how does it know that this is a asynchronous function? And where does it get pushed to so that the other two functions can be executed since they are relatively simple?

I think I answered the question myself but I keep confusing myself so if somebody could explain in extremely simple terms that would be great and sorry for the extremely stupid question.

Aman
  • 11
  • 3
  • 2
    It depends on what `reads an absolute huge file` is, exactly - for example, if you're in Node, there would be different answers for how `fs.readFile` and `fs.readFileSync` are treated - or are you thinking of something else? – CertainPerformance Jun 03 '22 at 23:42
  • Have you googled the JS event loop? I see several good sources there – Samathingamajig Jun 03 '22 at 23:42
  • @CertainPerformance what I mean by a huge file, is that it is just a file that has a lot of data. – Aman Jun 03 '22 at 23:43
  • 2
    Only asynchronous operations use the event loop. Regular in-line code doesn't. – Barmar Jun 03 '22 at 23:43
  • @Samathingamajig I did, but they are still confusing me, if you have some, can you share them? Maybe I have not seen those yet. – Aman Jun 03 '22 at 23:44
  • You might be interested in the "[Run to completion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#run-to-completion)" section of the Event Loop article on MDN. – Ivar Jun 03 '22 at 23:51

1 Answers1

0

Ordinary function calls are not pushed on the event queue, they're just executed synchronously.

Certain built-in functions initiate asynchronous operations. For instance, setTimeout() creates a timer that will execute the function asynchronously at a future time. fetch() starts an AJAX request, and returns a promise that will resolve when the response is received. addEventListener() creates a listener that will call the function when the specified event occurs on an element.

In all these cases, what effectively happens is that the callback function is added to the event queue when the corresponding condition is reached.

When one of these functions is called, it runs to completion. When the function returns, the event loop pulls the next item off the event queue and runs its callback, and so on.

If the event queue is empty, the event loop just idles until something is added. So when you're just starting at a simple web page, nothing may happen until you click on something that has an event listener, then its listener function will run.

In interactive applications like web pages, we try to avoid writing functions that take a long time to run to completion, because it blocks the user interface (other asynchronous actions can't interrupt it). So if you're going to read a large file, you use an API that reads it incrementally, calling an event listener for each block. That will allow other functions to run between processing of each block.

There's nothing specific that identifies asynchronous functions, it's just part of the definition of each function. You can't say that any function that has a callback argument is asynchronous, because functions like Array.forEach() are synchronous. And promises don't make something asychronous -- you can create a promise that resolves synchronously, although there's not usually a point to it (but you might do this as a stub when the caller expects a promise in the general case). The keyword async before a function definition just wraps its return value in a promise, it doesn't actually make it run asynchronously.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • So does this mean only async functions are added to the event queue and run to completing while the other functions are processed. So basically the event loop passes only async functions to the stack, and process the other function, and waits for the stack to return and adds it to the end of the queue. – Aman Jun 04 '22 at 00:42
  • Yes. The event loop runs the first function in the queue, it runs to completion synchronously, then it runs the next function in the queue, it runs to completion, and so on. Meanwhile, async operations in the background add their callbacks to the queue. – Barmar Jun 04 '22 at 02:16
  • Sounding like an idiot again, but what do you mean by this: Meanwhile, async operations in the background add their callbacks to the queue. – Aman Jun 04 '22 at 02:25
  • Like the browser receiving the response to an AJAX request, or the scheduled time of `setInterval()` or `setTimeout()` being reached. – Barmar Jun 04 '22 at 02:25