re: Explain coroutines like I'm five VIEW POST

TOP OF THREAD FULL DISCUSSION
re: 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 tha...
 

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.

 

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

 

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.

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.

 

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

code of conduct - report abuse