DEV Community

Cover image for async/await: under the hood

async/await: under the hood

Aaron Schlesinger on June 30, 2020

I'm really interested in concurrency strategies in programming languages, and because there's a lot of written research out there on the topic, you...
Collapse
 
ben profile image
Ben Halpern

Really well described

Collapse
 
arschles profile image
Aaron Schlesinger

Thanks Ben 😊

Collapse
 
danstur profile image
danstur

This post falls prey to an issue I find very common to people new to asynchronous code: Conflating asynchronous code with concurrent code. This leads to a lot of pain or at least confusion down the line and it's important to understand the difference.

Concurrency means that two or more threads of execution run concurrently - i.e. in parallel.

Asynchronous code on the other hand is a form of cooperative scheduling, usually implemented via continuation passing style (CPS). Simplified, async code doesn't mean that code runs in parallel just that you switch executing different code on the same thread.

You can combine the two concepts, which is often done, but you don't have to: You can have asynchronous code in a single-threaded program without any problems. Hell you can await a task/promise/whatever you want to call it without ANY thread being involved!

Collapse
 
michaeltharrington profile image
Michael Tharrington

This seems like it's probably helpful advice, but the line "this post falls prey to an issue I find very common to people new to asynchronous code" is condescending and unnecessary. Next time, I'd advise just giving the feedback without categorizing the mistake as something that people new to asynchronous code make.

Collapse
 
jessekphillips profile image
Jesse Phillips

However your comment falls prey to using concurrent and parallel synonymously where by async/await is in fact concurrent but not parallel.

medium.com/@itIsMadhavan/concurren...

A system is said to be concurrent if it can support two or more actions in progress at the same time. A system is said to be parallel if it can support two or more actions executing simultaneously.

Collapse
 
danstur profile image
danstur • Edited

@jesse Interesting definition and I can absolutely see the value in distinguishing between the two that way.

That doesn't seem to be the only definition though and not the one I'm used to. If you look at say the C++ memory model definition in the standard or Java Concurrency in Practice (to name one seminal book in that area) both use "concurrently" meaning "parallel".

e.g. from the standard: "Thus a bit-field and an adjacent non-bit-field are in separate memory locations, and therefore can be concurrently updated by two threads of execution without interference"

Thread Thread
 
jessekphillips profile image
Jesse Phillips

Yeah, I'm not too fond of trying to make a distinction for those terms... I always have to look up which one is which. But asynchronous has the same issue.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Totally agree, the concurrency is only in the fact that there are multiple "stacks" of things that will happen on continuation. Using async code to perform collaborative tasks is possible but a blunt instrument. I did some stuff around a more fine-grained collaborative multitasking that I talk about here:

Collapse
 
dansimiyu profile image
dan-simiyu

Simplified and clear explanation. Thanks man

Collapse
 
triptych profile image
Andrew Wooldridge

Thanks for this!

Collapse
 
buistvoploti profile image
American Psycho

clickbait title

Collapse
 
sanchitc99 profile image
Sanchit Chaudhary

Great,looking forward to read your other articles too!