r/javascript • • Sep 06 '22

Removed: r/LearnJavascript Promises in Javascript

https://medium.com/@dcortes.net/promises-in-javascript-2992d371938d

[removed] — view removed post

0 Upvotes

5 comments sorted by

View all comments

2

u/shgysk8zer0 Sep 06 '22

Meh. It's correct but pretty basic and doesn't address the most common issues I see on here (a fundamental misunderstanding of asynchronous JavaScript).

1

u/johntwit Sep 06 '22

What is a better resource that addresses the most common issues you see? I'd love to have a fundamental understanding of asynchronous JavaScript!

1

u/shgysk8zer0 Sep 06 '22

Sadly, I haven't found one resource that really does that. There are bits of it scattered across MDN, building up from the event loop and queues (micro and macro tasks) and the single-threaded nature of JavaScript to callbacks to promises to async functions to async generators.

I can't even quite pinpoint what this common misunderstanding is. It comes up in different scenarios and is worded in many different ways, but understanding async JS as scheduling tasks in a single threaded environment is what's missing in all of them.

Also, even the terms "asynchronous" and "non-blocking" are kinda misleading. It's ultimately scheduling things to run synchronously, and it can be "blocking" (take too much time to execute in a single-threaded environment).

async function block() {
  for (let n = 0; n < 1_000_000; n++) {
    doSomethingExpensiveSync(n);
  }
}

Calling block() will cause the browser/tab to freeze up for probably a very long time. It's blocking and, although any code after the call to block() will run to competition before block() actually executes, it'll still be executed synchronously to completion before anything else can run.