1

I'm confused about synchronous JavaScript vs asynchronous. If by default JavaScript is single threaded and can only complete one operation at a time, line by line, wouldn't this be 'asynchronous' i.e. things do not occur at the same time? How is it synchronous?

Also a piece of async code like a promise, the promise allows the rest of the code to run while it waits to resolve. Wouldn't this be synchronous i.e. letting multiple operations happen at once?

I'm confused as this seems the wrong way round in my mind.

otwebdev
  • 27
  • 1
  • 3
    You've got synchronous and asynchronous reversed. Synchronous, in terms of computer science and engineering, is sometimes easier described as *sequential* - i.e., an operation waits for the previous to complete. Whereas asynchronous refers more to two operations running in parallel - that is, tasks can be performed without waiting for the previous task to finish. – Tyler Roper Apr 18 '19 at 02:12
  • Your question possibly was already answered: https://stackoverflow.com/questions/2035645/when-is-javascript-synchronous – ABC Apr 18 '19 at 02:13
  • Possible duplicate of [When is JavaScript synchronous?](https://stackoverflow.com/questions/2035645/when-is-javascript-synchronous) – ABC Apr 18 '19 at 02:13
  • @TylerRoper I'm confused because the definition of synchronous is 'existing or occurring at the same time.', if JavaScript can only process one operation at a time, how is it synchronous? that's what's confusing me – otwebdev Apr 18 '19 at 02:15
  • @otwebdev Understood. I actually just updated my comment with some better information just before your comment. Please [see this](https://stackoverflow.com/a/748189/2026606) for a better explanation, and be sure to eye the comments. – Tyler Roper Apr 18 '19 at 02:16
  • @TylerRoper well that's why I was confused then. By their standard definitions it seems like the meanings are reversed? – otwebdev Apr 18 '19 at 02:17
  • 2
    @otwebdev Tom Padilla has a comment in regards to that in the link provided above: *"Oddly enough "Synchronously" means "using the same clock" so when two instructions are synchronous they use the same clock and must happen one after the other. "Asynchronous" means "not using the same clock" so the instructions are not concerned with being in step with each other. That's why it looks backwards, the term is not referring to the instructions relationship to each other. It's referring to each instructions relationship to the clock. Hope that helps."* – Tyler Roper Apr 18 '19 at 02:18
  • 1
    @TylerRoper - some dictionaries actually state, in computers, that asynchronous means *having each operation started only after the preceding operation is completed* - so the confusion is quite justified!! And wikipedia has `Asynchrony, in computer programming, refers to the occurrence of events independent of the main program flow and ways to deal with such events` – Jaromanda X Apr 18 '19 at 02:20
  • @JaromandaX I only felt confident enough to chip in my two cents because I remember how dizzy I was myself when trying to learn and understand it :) I think I may have asked the same exact question as OP a few years back haha. I definitely sympathize with the confusion! – Tyler Roper Apr 18 '19 at 02:21
  • 1
    @TylerRoper - wasn't saying you were wrong - just pointing out that inconsistency in dictionaries (who should stay clear of computing terms :p ) – Jaromanda X Apr 18 '19 at 02:23
  • Possible duplicate of [Asynchronous vs synchronous execution, what does it really mean?](https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean) – Paula Livingstone Apr 18 '19 at 07:33

1 Answers1

1

Something can be both single-threaded and asynchronous.

Firstly, let's talk about the difference between a thread and a process.

The basic definition is that separate processes have seperate memory spaces - they can't access each other's memory.

Whereas separate threads share the same memory space.

If we think of a thread as a queue of instructions, a multithreaded application can have two of these queues of instructions operating at the same time, but each accessing the same memory (and potentially screwing up the the state of the memory for the other thread.).

JavaScript is single threaded

All this means is that there is this one queue of instructions.

Now, this does that mean that JavaScript might not be suitable for doing embarrassingly parallel processing like sorting a trillion numbers using the quick sort algorithm, because you can't make use of a computer's multiple processors.

So how does the asynchronous work?

It comes down to the JavaScript event loop, and the fact that JavaScript is non-blocking.

To give an example, if I write some code that looks like:

const response = await fetch("/api/someData"); 

or not using async/await:

    fetch("/api/someData").then(response => {
        //Use the response here. 
    }); 

And say it takes one second for this response to return, the JavaScript engine doesn't just sit there doing nothing until the reponse returns.

Instead, the event loop continues and it continues processing everything else that it can, until the promise resolves and that code can continue.

If you want more details on exactly how the event loop works, I recommend reading that Mozilla documentation, or this post.

dwjohnston
  • 11,163
  • 32
  • 99
  • 194