0

I mean, this may seem like a silly question, but I don't think it really is. In programming languages where we can use threads, the concept is very simple. But Javascript is single-threaded and handles events through callbacks. What really makes something "branch-out" the execution process (I know this is the wrong technical term but I hope you understand my point). Is it the timers? Like setTimeout and setInterval?

I mean, if I were to write a Database with JS (I know JS wouldn't fit it but please bear with me for the sake of the example) and I need to check for some hardware operations to be completed before calling a callback function to handle the completion of such operation I cannot for the sake of me think in a possible way to actually make this 'pseudo-code' async. I mean:

function op() {
   while (someHardwareOpIsNotCompleted()) doWhatever();
   callback("done");
}

How can I make this function run asynchronously without using some form of timer that refers and 'counts' to a different thread? In my mind the only way around it is to pass to setTimeout the function reference as such setTimeout(op). From here on I can implement my callbacks and listeners, or the syntax-sugar Promise or even the syntax sugar-sugar "async".

Hopefully I was able to get my point across and I don't get replies like: "Use promises!" or something of the sort. If the real structure has nothing to do with timers but only simple callbacks, can someone give me an example on how to achieve a non-blocking execution of some piece of code without timers?

Thanks

Bruno Giannotti
  • 323
  • 2
  • 13
  • 1
    What you are referring to is called "Event Loop" and has a couple of threads on StackOverflow. Take a look at this one: https://stackoverflow.com/questions/21607692/understanding-the-event-loop – Marko Jun 08 '20 at 19:45
  • I would suggest you to see this video explaining how the event loop works. https://www.youtube.com/watch?v=cCOL7MC4Pl0 – Ahmed Hammad Jun 08 '20 at 19:46
  • There's a programming term for when a single thread gives multiple processes shared time, but for the life of me I cannot remember it. – Taplar Jun 08 '20 at 19:47
  • I thought I knew about the event loop until I started really thinking about it lol. But then, even `setTimeout` needs to call a core async function somewhere in order to actually "perform async", right? – Bruno Giannotti Jun 08 '20 at 19:50
  • 1
    Maybe [this](https://stackoverflow.com/a/59519051/5459839) helps... – trincot Jun 08 '20 at 19:54
  • 1
    It depends on the hardware. How are you accessing it? Often enough, the operating system itself does provide an asynchronous API for it. If a synchronous `someHardwareOpIsNotCompleted` is all you have, a timer-based polling loop is indeed your only choice. – Bergi Jun 08 '20 at 20:21
  • Thanks Bergi, thats what I wanted to know. – Bruno Giannotti Jun 08 '20 at 20:24

1 Answers1

0

A function is asynchronous for one of two reasons.

  • It needs to happen later (e.g. when a timeout has finished or when a click happens)
  • It takes a long time and shouldn't tie up the JS engine while it runs (e.g. accessing a database / file / network resource)

The asynchronous parts of the latter functions are not written in JavaScript. They are provided by the host environment and are usually written in the same language a that (e.g. C). The host environment exposes an API to provide access to them to the JavaScript.

For example, the source code for Chrome's implementation of XMLHttpRequest is written in C++.

If you need to poll something, then testing it on a timer is the usual way.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335