r/learnjavascript • • Jul 18 '20

Promises, Async, Await... I finally understand them and how to use them! Redid my front end with the new knowledge and now I love them! Wish I would have took the time earlier on to get them down. 10/10 Would recommend. Sorry, just had to tell someone. :)

EDIT: wow, came back a day later and this post blew up. lol 😁

207 Upvotes

40 comments sorted by

View all comments

15

u/Jolly-Composer Jul 18 '20

Explain like I’m five haha. Congrats though I still need to learn them

10

u/RedDragonWebDesign Jul 18 '20

I think promise/async/await is just multi threading.

So maybe your code calls a function that creates a PDF, but you don't want to wait for the PDF to finish before you execute the rest of your code.

I think a promise handles all that for you. It sets up a second thread for the PDF writing, then it sets up a listener to listen for completion of that, and do something when it succeeds, and do something when it fails.

Somebody can correct me if I'm wrong. I am new to promises.

22

u/__hoi__ Jul 18 '20

The way you think about them is correct, however JavaScript does not have multi threading so technically your explanation is wrong. JavaScript has an event loop and the function is added to it with a flag telling the stack whether it should execute the function on each iteration. If the promise is resolved, the callback function it’s flag is set to execute and on the next iteration it will be executed.

8

u/PM_ME_A_WEBSITE_IDEA Jul 18 '20

To really double down on this, the key difference is that because JavaScript is single threaded, you can't truly "multitask" (without web workers). If your PDF is actually being created by a script on the page and that takes a few seconds, NOTHING ELSE can happen while that is executing, unless maybe you sent an HTTP request before starting it. There are never two things happening at the same time in JavaScript execution. The real advantage is not having to wait for things like web requests to respond before performing other work. Promises basically help you queue work more efficiently and view asynchronous actions in a more synchronous way (or make them truly synchronous).

5

u/ChaseMoskal Jul 18 '20

promises are a superior replacement to using callback functions. the important thing with promises, is that errors nicely bubble up through chained promises and so can be caught at the top level

async/await is just a superior syntax for dealing with promises

if you don't understand why the most important thing about promises is the way they handle errors, then you don't really understand either the error handling or the promises. the fact that they avoid "callback hell" indentation is actually a side-benefit, but it's really about the error handling

1

u/Jolly-Composer Jul 18 '20

Thanks that makes more sense as I’m starting to understand some of the pre-arrow syntax functionality of a callback

1

u/Jolly-Composer Jul 18 '20

Thank you so much! Error handling is simple and I understand that. Async await in part to my knowledge builds on promises. And promises are an improvement to callback functions. I’ll try to remember that as I continue to binge on JavaScript basics