r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

135

u/theMightyPanda27 May 28 '19

Promises. They are quite tricky to understand for a beginner, but very convenient as one masters.

29

u/senocular May 28 '19 edited May 28 '19

Normally I would agree with you, but lately that doesn't seem to be the case.

I've been doing a lot of interviews lately and probably the one thing people have consistently showed a keen understanding of is promises. Where people have slipped up the most? this. While I get this is tricky (it's already been brought up a few times in this thread) I've also always considered promises to have a bit of a learning curve, especially compared to the more classic callback approach. But I guess learning material nowadays, and I guess apis in general, use them so much that people need to learn them a lot sooner than before...? Not sure, but I thought that was a little interesting takeaway from these interviews.

Edit: This said with a sample set of "advanced beginners"

0

u/[deleted] May 29 '19

[deleted]

3

u/senocular May 29 '19

...unless the function was created with bind() or an arrow function. And then you have changes of behavior based on strict mode...

There's another part of this thread where someone argues that this isn't confusing, but I'm not sure there's anything else as complicated as it is in JS. So relatively speaking, it's up there.

→ More replies (3)

0

u/[deleted] May 28 '19

[deleted]

26

u/[deleted] May 28 '19

However important to note that async/await is just syntactic sugar around Promises. The syntax is nice but sometimes not as flexible. And an async function is just a function that returns a promise, even if you never await anything inside it.

3

u/____0____0____ May 28 '19

Yes, exactly. I agree with both your points. I find async await makes a lot more sense in given contexts, where others it may not or you need additional functionality. I switch between the two depending on the given context.

2

u/alexbarrett May 29 '19

In a lot of languages the terminology is split into promises/futures which would make more sense to you.

→ More replies (1)

12

u/pm_me_cute_rem_pics js/ux May 28 '19

Yeah especially waiting for promises to resolve and parallel stuff with async/await.

For example i see this in a lot of my code reviews with new js programmers:

// does some parallel async stuff
arrayOfItems.forEach(async item => {
    await doSomethingAsync(item)
})

doSomethingWhenDone()

This won't wait for the promise to actually resolve outside of the forEach and call doSomethingWhenDone(). If your program exist it will not wait for your async work to be done.

either do:

const pendingPromises = []

arrayOfItems.forEach(item => {
    pendingPromises.push(doSomethingAsync(item))
})

await Promise.all(pendingPromises)

doSomethingWhenDone()

Or if you like mapping over promises.

await Promise.all(
    arrayOfItems.map(async item => {
        await doSomethingAsync(item)
    })
)

doSomethingWhenDone()

examples https://repl.it/repls/WeeklyColorfulGraduate

→ More replies (9)

10

u/amdc !CURSED! May 28 '19

knowing Promises is essential if you want to succeed, but once you're familiar with them you should move on to async/await

5

u/SharkLaunch May 29 '19

Async/await are just syntactic sugar around promises, and there are times when the former is more appropriate.

2

u/amdc !CURSED! May 29 '19

Out of interest, what are cases where promises are more appropriate (besides .race and .all which you totally can use in conjunction with await)?

→ More replies (1)
→ More replies (7)

2

u/BehindTheMath May 28 '19

This is one of the most common misunderstandings I see in beginner questions posted here and /r/webdev. Really any async code, including callbacks.

→ More replies (1)