News for hackers. Stuff that matters.
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.