News for hackers. Stuff that matters.
Re:A Modern JavaScript Tutorial by nailer on Monday December 07, 2020 @12:08PM (#25334115)
The default way promises are used is now async/await. `.then()` should not be introduced as anything more than 'you may find this in older codebases'.
Re:A Modern JavaScript Tutorial by throw_m239339 on Monday December 07, 2020 @12:38PM (#25334526)
> The default way promises are used is now async/await. `.then()` should not be introduced as anything more than 'you may find this in older codebases'.

No, because you might have to write explicit promises in the browser with async API that are callback based.

You need to understand how promises work to use async/await at first place anyway.

Just like it's better to understand how prototypal inheritance works in JS BEFORE even using a single class declaration.

Re:A Modern JavaScript Tutorial by hutzlibu on Monday December 07, 2020 @01:01PM (#25334876)
"Just like it's better to understand how prototypal inheritance works in JS BEFORE even using a single class declaration. "

About that, does JS class inheritance now work like classes in java for example? Or is it still just syntactic sugar for the prototype inheritance? By your wording, I assume the later?

Re:A Modern JavaScript Tutorial by ragnese on Monday December 07, 2020 @01:55PM (#25335669)
Correct. It's still prototype based. Thus making the class keyword actually MORE confusing for people from other languages, IMO.
Re:A Modern JavaScript Tutorial by wa1987 on Monday December 07, 2020 @01:34PM (#25335375)
Re:A Modern JavaScript Tutorial by hutzlibu on Monday December 07, 2020 @01:50PM (#25335591)
Wow. I was hoping, at some point things would improve a bit.
Re:A Modern JavaScript Tutorial by wa1987 on Monday December 07, 2020 @01:57PM (#25335692)
Agreed. I find myself using a pretty small subset of the language which definitely doesn't include the class keyword. Doubt I'll ever find it useful.
Re:A Modern JavaScript Tutorial by hutzlibu on Monday December 07, 2020 @02:25PM (#25336035)
Yeah, like many people, I ended up making my own inheritance routines. Then when class came out, I was half rejoicing, half regretting to put up the work. But well, no thanks. I rather keep using my own far from perfect way of things.
Re:A Modern JavaScript Tutorial by nailer on Monday December 07, 2020 @11:58PM (#25342039)
Yes because you can and should promisify old libraries.
Re:A Modern JavaScript Tutorial by throw_m239339 on Tuesday December 08, 2020 @02:01AM (#25342679)
No because some async Web API are just a bit more complex than just 'promisify' this or that.
Re:A Modern JavaScript Tutorial by nailer on Tuesday December 08, 2020 @06:18AM (#25343930)
Which ones?
Re:A Modern JavaScript Tutorial by ericwood on Monday December 07, 2020 @12:19PM (#25334263)
Maybe some codebases mandate it, but it's very common to use either style when it makes the most sense. The classic syntax is by no means deprecated or superseded by async/await.
Re:A Modern JavaScript Tutorial by golergka on Monday December 07, 2020 @12:26PM (#25334360)
async/await is excellent syntax when you want things to happen one after another. That's the usual case, and that's what I find myself using 95% of the time.

However, when you want a more fine-grained concurrent execution of different asynchronous things, then() still could be very useful.

Re:A Modern JavaScript Tutorial by postalrat on Monday December 07, 2020 @01:16PM (#25335101)
Can you give an example of more fine grained control with then?
Re:A Modern JavaScript Tutorial by wa1987 on Monday December 07, 2020 @01:44PM (#25335492)
Often times operations are order-independent. Specifying those as sequential `await`-s does not make a lot of sense in those situations. I guess less fine grained control is desirable under such circumstances :-)

There's also other useful utilities such as Promise.all(), Promise.any() and Promise.race().

Re:A Modern JavaScript Tutorial by golergka on Monday December 07, 2020 @01:40PM (#25335448)
const [result1, result3] = await Promise.all([task1(), task2.then(res => task3(res))])
Re:A Modern JavaScript Tutorial by postalrat on Monday December 07, 2020 @02:43PM (#25336281)
Could that be written

  const task2then3 = async () => task3(await task2());
  const [result1, result3] = await Promise.all([task1(), task2then3()])
Re:A Modern JavaScript Tutorial by nailer on Tuesday December 08, 2020 @06:29AM (#25343982)
I'd just:

    async function getResult3(){
      const res = await task2()
      return task3(res))
    }

    const [result1, result3] = await Promise.all([task1(), 
    getResult3()])
Re:A Modern JavaScript Tutorial by verisimilidude on Monday December 07, 2020 @12:32PM (#25334456)
That’s probably good general advice.

One problem. I’m increasingly coming across devs who’ve never used Promises directly, only async/await. They’re introducing all kinds of problems into code because they don’t have that foundational understanding of Promises. I think it’s essential to know how Promises work, even if we generally recommend async/await.

Also, Promise interface can do things that async/await cannot, such as Promise.all, and other unusual async execution scenarios. It’s important to have those tools in your belt.

Re:A Modern JavaScript Tutorial by nailer on Monday December 07, 2020 @01:05PM (#25334926)
100%, people should definitely understand Promise.all() and Promise.any().
Re:A Modern JavaScript Tutorial by nailer on Tuesday December 08, 2020 @06:23AM (#25343955)
> Promise interface can do things that async/await cannot, such as Promise.all

PS I should clarify await and Promise.all/any work fine:

    await Promise.all(items)
Re:A Modern JavaScript Tutorial by postalrat on Monday December 07, 2020 @01:19PM (#25335147)

  (async () => { set(await get()); })();
 
  get().then(r => set(r));
Ignoring error handling I'll often choose the second option.
Re:A Modern JavaScript Tutorial by Rumudiez on Monday December 07, 2020 @02:24PM (#25336023)
Much cleaner. You can go even further by using point-free style:

    get().then(set)
It's the try/catch blocks that make async/await truly worse than native promises. I use them sparingly because so often the .then syntax is both clearer and terser.
Re:A Modern JavaScript Tutorial by postalrat on Monday December 07, 2020 @02:45PM (#25336296)
What don't you like about try/catch? I find the opposite true, the .then/.catch is more confusing.
Re:A Modern JavaScript Tutorial by Rumudiez on Monday December 07, 2020 @03:46PM (#25337251)
try/catch introduces new block scopes and interrupt control flow. I find this example very readable, but expanding it into try/catches would make it rather long winded and harder to see the pattern at work as you extend the sequence:

    get()
        .then(handleResult)
        .catch(useFallbackResult)
        .then(updateState)
I prefer concision and functional expressiveness because it reduces the surface area for writing buggy code. Much like how using map instead writing a for loop can eliminate out of range index bugs, chaining promises brings useful guarantees about how the code I do write can be interpreted.
Re:A Modern JavaScript Tutorial by nailer on Tuesday December 08, 2020 @06:19AM (#25343935)
OK. I'll do:

  const result = await get()
  set(result)
But you do you.

Current node and console have top level await BTW.

Re:A Modern JavaScript Tutorial by postalrat on Wednesday December 09, 2020 @10:25AM (#25359432)
That only works in an async function. The then example doesn't have that requirement. So the alternative shouldn't either.
Re:A Modern JavaScript Tutorial by nailer on Wednesday December 09, 2020 @01:36PM (#25362136)
Yes, your example isn't complaining about then() being clearer than await (because it isn't) but rather complaining that top level await isn't everywhere yet.

In other words, your complaints have disappeared in node, and will soon in browsers: https://github.com/tc39/proposal-top-level-await

So yes:

    set(await get())
Re:A Modern JavaScript Tutorial by postalrat on Friday December 11, 2020 @12:08PM (#25387998)
Sometimes you may need to handle a promise in a function that can't be made async.
Re:A Modern JavaScript Tutorial by nailer on Friday December 11, 2020 @05:03PM (#25391863)
If a function is inherently sync, then it shouldn't be using promises.
Re:A Modern JavaScript Tutorial by jedimastert on Monday December 07, 2020 @12:14PM (#25334190)
Can you point to anything other than saying "this is obvious"?
Re:A Modern JavaScript Tutorial by nailer on Monday December 07, 2020 @12:18PM (#25334251)
No, just experience with a lot of codebases.

Here's a simple question to ask yourself: what if non-IO operations allowed you to use the .then() syntax? If you have the choice to use:

   1 + 1.then(function(result) { ... }
vs

   const result = 1 + 1
Which would you choose?
Re:A Modern JavaScript Tutorial by pavlov on Monday December 07, 2020 @12:24PM (#25334327)
Using then() doesn't pause execution of the callsite function, while await does. Surely that's a relevant difference here?

It doesn't seem unreasonable to use then() to express: "Run this anonymous function whenever the async thing completes. Meanwhile let's do this other stuff right now."

Re:A Modern JavaScript Tutorial by nailer on Monday December 07, 2020 @12:59PM (#25334842)
Both codebases can't do anything until `result` has a value.