DEV Community

Discussion on: Explain coroutines like I'm five

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

Async, concurrecy, coroutines or goroutines, from 10.000 feets in the air they are synonyms, the devil is in the detail but you haven't reached that stage I presume.

You are 5Y old, but you are already using a tablet. It's evening and you are in a hurry, you have to finish your homework (on the tablet).
Also a your sister asked you to help her in a game.
Tonight is also some live streaming cartoons.
You open all the apps on the tablet, and juggle between them. You cannot do 2 things at once, you can only do 1 thing at a time (homework, watch cartoons or play the game).

Your attention == 1 core CPU
The current app == the current task/function
The tablet == running tasks

This is concurrency/async, you will finish all 3 tasks, in the same time span (this evening), by running a bit of each for a few minutes and then switch. From an outsider perspective (the User/programmer), you do all the 3 tasks in paralell.

The CPU does the switching so fast that it seems paralel, but is not. If your attention is disturbed, and instead of that 3minutes you stay 10min on a specific tasks, the entire thread is blocked. The best example in JS is if you block the main thread with a heavy operation, the page will freeze from the user points of view (because the user actions triggered events which are not handled because the thread is busy).

You can have multiple threads/tasks, but if you only have 1 Core, there is only 1 processator, so all of them will wait in line to be processed.


Now, paralelism ... is when you call your friend, with his own tablet , and put him to do your homework, and you remain with only 2 tasks (play and watch). Now you have 2 paralel tasks (threads) on 2 CPU cores, working side by side.

I made a collection of more advanced JavaScript topics, one of them is concurrency and async, I recommend check them out (especially the Aridanm and Event loop videos).

Collapse
 
idanarye profile image
Idan Arye • Edited

I think your explanation is missing the main appeal of async - that you don't need to block on IO.

To build on your story, I'd put it something like this:

  • You start watching the cartoon, but it's the intro.
  • Instead of watching the intro you switch to the game and enter the online lobby - but it needs 3 players and only you and your sister are in it.
  • Instead of waiting for another player to join you switch to your homework, and answer the first question.
  • The second question has a link to a YouTube video you need to watch. You open it - and it starts loading.
  • Instead of waiting for it to load, you switch back to the cartoon. The intro is over, so you can watch.
  • Now there are commercials - but meanwhile a third player has joined so you switch to the game

And so on...

The idea is that you don't just switch the tasks really fast to make it look like you are doing everything at once. You utilize the time you are waiting for something to happen(IO) to do other things that do require your direct attention.

Collapse
 
tiffany profile image
tiff

This. This makes so much sense. Really great explanation.

Collapse
 
bgadrian profile image
Adrian B.G.

True, your completion explain how they should be used, in a correct way.

Collapse
 
thibmaek profile image
Thibault Maekelbergh

Thank you, this was clear enough to grasp it a bit better. I thought coroutines were a specific pattern for async operations though, but from above comments, it seems like they're roughly the same thing.

Thread Thread
 
idanarye profile image
Idan Arye

No - your were right at first. Coroutines are one way to do async - there are other ways. There are also other uses for coroutines. I've written a detailed reply to the main post.

Collapse
 
berserker1 profile image
Aaryan Bhagat

So couroutine is code based context switching, where you write the code and you write the code for context switching (where a function will stop and pass the execution to the caller).