- What exactly does this mean?
- How does it differ in comparison to multi-threaded/asynchronous execution?
Top comments (2)
Js code can be synchronous or asynchronous, but it does not normally run in parallel (in other words, it is single threaded):
Synchronous: Anna asks her parents for a cookie. The parents start to bake some cookies while Anna stands there frozen, doing nothing.
Asynchronous: Anna asks her parents for a cookie. The parents say the cookies are in the oven. Anna goes to play with blocks until the oven bell rings, notifying her that the cookies are ready.
Asynchronous: Suzy or Jimmy can play with the blocks one at a time, but not simultaneously. While Suzy walks to the toy bin to get a new block, Jimmy can play with the blocks, and vice versa, but they can't actually put blocks on top of one another at the same time.
It is possible for Suzy and Jimmy to play with the blocks simultaneously, but it requires special support, for example, using Web Workers
Here's the 5 year-old's explanation:
Translations to adult language are written like this.
Today, you went to the beach, and you did loads of things like:
You did all of these things one after another, because you can't do multiple things at the same time.
I made a necklace off 5 of the shells you found that you can show off to your friends at school. It has one shell for each of the things you did. Then you can poit at your shells from left to right when telling your friends about the trip, so you don't foret to talk about anything you did.
You can see the thread going through the shells as the **thread of execution. One thing is being done, then the next, then the next, and so on.
After you asked your dad for an ice cream conde, you went looking for shells. You didn't just wait for him to return. And when he came back with the ice cream, he had to wait until you found your last shell before giving you the ice cream.
findShells()in the code, it only runs at the end, when you've received the event of Daddy returning with the ice cream.