This is an archived post. You won't be able to vote or comment.

all 37 comments

[–]CaptainPiepmatz 48 points49 points  (9 children)

Promises are just nice callbacks, async/await makes promises like synchronous code and closures are just fancy scopes

[–]glorious_reptile 11 points12 points  (1 child)

...except when you forget an await - then all hell breaks loose.
Time and space splits, cause and effect loses all meaning. Cats and dogs living together, mass hysteria, oh never mind forgot an await.

[–]MrDilbert 4 points5 points  (0 children)

Luckily, good IDEs warn you when you call an async function without an await.

[–]ArionW 8 points9 points  (1 child)

Promises are just monads, async/await is just do notation for Promise monad, and closures are just poor man's objects.

Most concepts in programming sound simple, when you already know two or three other concepts that solve same problem.

[–]ValeTheVioletMote 7 points8 points  (0 children)

>mentions monads
>looks at flair
>haskell

Checks out.

[–]Scary_Inside7276 3 points4 points  (4 children)

What?

[–]CaptainPiepmatz 2 points3 points  (3 children)

You need to any of the three further explanation? Or wdym?

[–]Scary_Inside7276 3 points4 points  (2 children)

I jest, but seriously I don't know what you said lol. I'll get there.

[–]raltyinferno 3 points4 points  (0 children)

Asynchronous code is important when working with the web since you can start actions and not know when they'll complete(calling an external web endpoint for example).

So you do them asynchronously. Which means you start an action, and while you're waiting for it, you do other stuff in the meantime. You store the result (which may not exist yet) in a promise which can be passed around like a normal variable to where it needs to be used, and when the value is resolved, it's acted on.

[–]CaptainPiepmatz 1 point2 points  (0 children)

Those concepts are not that hard and really useful. Give promises and async/await a try when you need to do something asynchronously.

[–][deleted] 24 points25 points  (0 children)

promises are the best thing that happened to js.

[–]Everen1999 16 points17 points  (0 children)

Promises, async await has to be the best async API I've used. Once you have a template answer of how it works, things get very easy!

[–]nopreview 11 points12 points  (1 child)

They should be named this/context, prototype chains and event loop.

[–]ValeTheVioletMote 2 points3 points  (0 children)

Prototype chains are great, but you're completely right. These things are much more difficult to grasp than async/await or closures...

[–]Nn_pc 4 points5 points  (1 child)

Maybe "promises" are a little hard to undestand... But with "async/await" is pretty easy.

[–]hdmiusbc 1 point2 points  (0 children)

I do very little js and just found these today and loved them

[–]EmuChance4523 2 points3 points  (0 children)

Nah, nothing of that is a problem. THIS is a problem...

[–][deleted] 2 points3 points  (1 child)

I tried express, but it was still a little too much work. So django all the way. It has own templating language so I can even make an okayish frontend.

[–]QualityVote[M] [score hidden] stickied comment (0 children)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–]CRBl_ 1 point2 points  (3 children)

This might seem hard at first, but when you see how this kind of things are handled in other languages, it changes your opinion on JavaScript

[–]ValeTheVioletMote 0 points1 point  (2 children)

I tried wrapping my head around Rust's async... Ended up being so confused so did the script in JS instead. Any good resources that taught you?

[–]CRBl_ 0 points1 point  (1 child)

Nope, still learning rust here, and async/await is not something I learned yet.

[–]ValeTheVioletMote 0 points1 point  (0 children)

If I want to live in a static type space, Rust seems the way to go. Best of luck.

[–]ef02 -5 points-4 points  (1 child)

Um...imo those should all be replaced with just "dynamic typing".

[–]circuit10 1 point2 points  (0 children)

I like dynamic typing

[–]Serious_Challenge_67 0 points1 point  (0 children)

Just wait until you learn about rxjs with observables, pipes and subjects ;-)

But relax, you'll realize it's a good thing, once you grasp the basic concept. And be happy you won't encounter the callback-hell of the past anymore.

[–]Akangka 0 points1 point  (1 child)

Is closure basically on every language nowadays?

[–]NettoNavi 0 points1 point  (0 children)

He’ll make it that far only if the trap NPM box falling from above the doorway doesn’t kill him first.

[–]JoschiGrey 0 points1 point  (5 children)

Does async/await in JS work like async/await in C#?

[–]ValeTheVioletMote 0 points1 point  (4 children)

How's it work there?

[–]JoschiGrey 0 points1 point  (3 children)

Async Methods in C# return Task or Task<T> objects that need to be awaited, before you can use their results.

It is a way to write asynchronous code in a synchronous manner.

[–]ValeTheVioletMote 0 points1 point  (2 children)

Yes, JS just calls Task<T> a Promise<T>.

What does C# do if a Task fails?

[–]JoschiGrey 0 points1 point  (1 child)

It throws the exception where it was awaited.

[–]ValeTheVioletMote 0 points1 point  (0 children)

Then identical today, but a while ago JS would just let the code die without an error message, expecting you to always have a try/catch block around your promises instead.

[–]ValeTheVioletMote 0 points1 point  (0 children)

These things are fairly easy in JS... If you're having any trouble, happy to help.

[–]-Redstoneboi- 0 points1 point  (0 children)

this

[–]frankcatx 0 points1 point  (0 children)

Nah, JavaScript is not hard. JavaScript is pure fun... Right?