I've been struggling with wrapping my head around asynchronous programming with callbacks, promises and async/await in JS, however I think it's finally clicking after watching these YouTube videos and creating a document where I explain these concepts as if I'm teaching them to someone else:
I'd argue that you cannot understand asynchronous programming in JS without knowing how the call stack in the JS engine, web APIs (provided by the browser or NodeJS as far as I know), the callback/(macro)task queue, microtask queue, and the event loop all fit together. The JS engine is a small component of that entire environment, and not being aware of it is like walking in the dark with dim candle.
The fact I didn't know about these concepts is the reason why I always struggled to understand examples involving setTimeout, setInterval, etc. Most videos on YouTube, even from well-known personalities in the programming YouTube community, simply jump into things without context at all even when the videos are titled "for beginners". Even some books and docs simply describe the spec without providing context and motivation. Edit... Forgot to say Philip does an outstanding job in that video. He's even humble enough to admit it took him a few months to grasp what was going on.
async programming IS hard to get your head around.
The resources you suggest are spot on. AND, async programming will really only click when you do lots of it. Read tons of async resources like the ones above, write lots of async code. One day you'll realise it's second nature.
> Thanks a lot, I'll keep this in mind and try to write as much async code as possible. Only write async code if the advantages outweigh the downsides, not just by default in order to do some training for yourself. Unless it is in a hobby project where training yourself is part of the goal.
Effective Python has a great chapter that goes over concurrency and parallelism using threads, coroutines and subprocesses. There are a lot of non-trivial code examples that the author gradually shows how to improve with more advanced language features over the course of several chapters so it's not all thrown at you at once, enabling you see benefits and pitfalls of approaching problems with different techniques. I highly suggest it even beyond learning async features.
I've realized it's so easy to read or watch something and fool yourself into thinking that you know the topic.
For example, coming up with your examples helps a lot. For instance, I found most of the examples quite underwhelming and confusing so I ended up creating my own asynchronous functions to demonstrate the concepts as I go along.
You're welcome! Rewatch Philip's video as many times as you need until the concepts sink in, before you move into learning promises and await/async [1]. Similarly for Jake Archibald's video.
Good luck! [1]: from what I gather asynchronous programming using callbacks isn't used anymore but it might be instructive to start with it, understand the issues with it, and why promises replaced this style.
I've your blog bookmarked and I'll be reading it this weekend haha. Thanks for putting it together!
>Callbacks are not asynchronous by nature, but can be used for asynchronous purposes. Glad you mention this because some people make it since like callbacks are a de-facto asynchronous-based concept. Callbacks are simply anonymous functions or lambdas that you pass to asynchronous functions so you can work with the produced values. Correct me if I'm wrong though.
Callbacks are anonymous functions [or references to named functions] that you pass to asynchronous [or synchronous] functions so you can work with the produced values [or do anything else]
* Philip Roberts's What the heck is the event loop anyway? - https://www.youtube.com/watch?v=8aGhZQkoFbQ
* The Story of Asynchronous JavaScript - https://www.youtube.com/watch?v=rivBfgaEyWQ
* JavaScript Callbacks, Promises, and Async / Await Explained - https://www.youtube.com/watch?v=JRNToFh3hxU
* Async Javascript Tutorial For Beginners (Callbacks, Promises, Async Await). - https://www.youtube.com/watch?v=_8gHHBlbziw
* Jake Archibald: In The Loop - setTimeout, micro tasks, requestAnimationFrame, requestIdleCallback, - https://www.youtube.com/watch?v=cCOL7MC4Pl0
Edit... I've been rewatching these videos, reading the MDN docs, the Eloquent JavaScript book, javascript.info, blogs about the subject, etc. This further proves you shouldn't limit yourself to a single resource, and instead fill up the laguna with water from different sources if you will.
The fact I didn't know about these concepts is the reason why I always struggled to understand examples involving setTimeout, setInterval, etc.
Most videos on YouTube, even from well-known personalities in the programming YouTube community, simply jump into things without context at all even when the videos are titled "for beginners". Even some books and docs simply describe the spec without providing context and motivation.
Edit... Forgot to say Philip does an outstanding job in that video. He's even humble enough to admit it took him a few months to grasp what was going on.
Continue this thread — 2 replies
Continue this thread — 1 reply
Or, you can upvote it (comment)
The resources you suggest are spot on.
AND, async programming will really only click when you do lots of it.
Read tons of async resources like the ones above, write lots of async code. One day you'll realise it's second nature.
>One day you'll realise it's second nature.
This is great but then the curse of knowledge hits you hard haha
> Thanks a lot, I'll keep this in mind and try to write as much async code as possible.
Only write async code if the advantages outweigh the downsides, not just by default in order to do some training for yourself. Unless it is in a hobby project where training yourself is part of the goal.
* Async Python in real life - https://guicommits.com/async-python-in-real-life/
* Python Concurrency: The Tricky Bits - https://python.hamel.dev/concurrency/
Tangentially, that is a marvelous way to learn something. I often find that in explaining a complex topic I uncover gaps in my own understanding.
For example, coming up with your examples helps a lot. For instance, I found most of the examples quite underwhelming and confusing so I ended up creating my own asynchronous functions to demonstrate the concepts as I go along.
Good luck!
[1]: from what I gather asynchronous programming using callbacks isn't used anymore but it might be instructive to start with it, understand the issues with it, and why promises replaced this style.
>Callbacks are not asynchronous by nature, but can be used for asynchronous purposes.
Glad you mention this because some people make it since like callbacks are a de-facto asynchronous-based concept. Callbacks are simply anonymous functions or lambdas that you pass to asynchronous functions so you can work with the produced values. Correct me if I'm wrong though.
Callbacks are anonymous functions [or references to named functions] that you pass to asynchronous [or synchronous] functions so you can work with the produced values [or do anything else]
Continue this thread — 1 more reply
Continue this thread — 5 more replies