News for hackers. Stuff that matters.
Re:I Avoid Async/Await by bin_bash on Saturday April 16, 2022 @08:05AM (#31051650)
I prefer async/await but this is a contrived example.

For starters you can just write:

    const myfunc = () => new Promise(resolve => resolve())
or

    Promise.resolve()
Re:I Avoid Async/Await by Aeolun on Saturday April 16, 2022 @08:53AM (#31051917)

   const myfunc = () => new Promise(resolve => resolve())
Is there anyone that actually likes this structure? I find it really hard to reason about what a line like this does. What is the upper limit on double arrows in one line?
Re:I Avoid Async/Await by bin_bash on Saturday April 16, 2022 @09:00AM (#31051960)
here's a real world example I use all the time:

    const wait = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms));
then:

    async function () {
      await wait(1000);
    }
Perhaps it's hard to grok but it sure is a handy one-liner.

Of course in newer versions of node we can now do:

    import {
      setTimeout,
    } from 'timers/promises';

    await setTimeout(100);
Re:I Avoid Async/Await by joe_guy on Saturday April 16, 2022 @11:44AM (#31053218)
There's no merit of what should be a project wide function being a single line instead of 3 to avoid "hard to grok" syntax.
Re:I Avoid Async/Await by deschutes on Saturday April 16, 2022 @12:28PM (#31053659)
Wait till you see the rest of javascript...
Re:I Avoid Async/Await by eurasiantiger on Saturday April 16, 2022 @05:36PM (#31056304)
Your comment is snarky and categorically dismissive. Care to explain why you have taken such an aggressive stance against JS?
Re:I Avoid Async/Await by love2read on Saturday April 16, 2022 @09:57AM (#31052363)
this works in older versions of node too:

const wait = require(‘util’).promisify(setTimeout)

Re:I Avoid Async/Await by oauea on Saturday April 16, 2022 @11:37AM (#31053144)
but not in the browser
Re:I Avoid Async/Await by eurasiantiger on Saturday April 16, 2022 @09:39AM (#31052223)
It’s the cleanest way to wrap a event-emitting library, for example. async/await is not applicable in such a case where resolve needs to be explicitly called in a callback.

    async function process() {
      return new Promise((resolve, reject) => {
        emitter.on(”error”, (err) => reject(err));
        emitter.on(”end”, (result) => resolve(result));
        emitter.start();
      });
    }
async is not strictly needed here, but I find it a good practice to use it on functions which immediately return a Promise anyway.
Re:I Avoid Async/Await by zkldi on Saturday April 16, 2022 @10:31AM (#31052629)
I don't think it's a good practice, atleast if you have typescript jn your stack. It's redundant and wraps the promise in another promise.
Re:I Avoid Async/Await by eurasiantiger on Saturday April 16, 2022 @11:14AM (#31052947)
IIRC that doesn’t actually happen if a native Promise is returned.
Re:I Avoid Async/Await by eyelidlessness on Saturday April 16, 2022 @12:33PM (#31053727)
It does, but only if you also add a redundant await. Which I still do, even in TypeScript, unless there’s a compelling performance reason not to. I disagree with the article overall, but I do agree that making asynchrony as explicit as possible is a good idea. Otherwise you end up with code like:

  async function foo(bar) {
    // ...
  }

  function nonObviousAsyncFn() {
    // ...
    return foo(quux);
  }
Explicit return types would help, but most people don’t write them unless they’re forced by a linter.
Re:I Avoid Async/Await by Aeolun on Sunday April 17, 2022 @06:13AM (#31059806)
This would still return a promise for both functions. My IDE at least is really obvious about what happens.
Re:I Avoid Async/Await by eurasiantiger on Sunday April 17, 2022 @03:24AM (#31059155)
Yes, this is a common anti-pattern

    return await foo();
Re:I Avoid Async/Await by albedoa on Saturday April 16, 2022 @01:20PM (#31054138)
Your gripe is that there are two arrows on one line? The second is an argument to the promise constructor. You should be able to reason about this line easily.
Re:I Avoid Async/Await by Aeolun on Sunday April 17, 2022 @06:11AM (#31059790)
I think I was unclear. Of course you can figure out what the line does. It’s just that you have to figure it out.
Re:I Avoid Async/Await by albedoa on Sunday April 17, 2022 @10:31AM (#31061087)
If the implication is not that the "double arrows" are tripping you up, then yes you were unclear.
Re:I Avoid Async/Await by wildrhythms on Saturday April 16, 2022 @08:28AM (#31051766)
As soon as you need to chain Promises, this gets very annoying and you quickly find yourself in a nested callback hell.
Re:I Avoid Async/Await by bin_bash on Saturday April 16, 2022 @08:53AM (#31051919)
If you’re nesting “new Promise” you’re almost certainly doing it wrong. This is for generating new promises not executing them.