top | item 41239583

(no title)

mrozbarry | 1 year ago

I prefer the raw promise syntax. It makes the promise chain look more like what it is, an asynchronous pipeline, and you don't need to litter async everywhere.

    const myAsyncThing = async (init) => {
      let value = await task1(init);
      value = await task2(value);
      return value;
    }

    const myPromiseThing = (init) => task1(init).then(task2)
I know this is a relatively simple and contrived example, but there are some times where async/await very much is bulky and gets in the way of just writing code.

discuss

order

THBC|1 year ago

That’s not ”raw Promise” syntax, that’s Thenable syntax.

Compare with

    const myPromise = new Promise((resolve, reject) => resolve(”foo”));
    
Afterwards either try-catch await myPromise, or use myPromise.then().catch()

saurik|1 year ago

I mean, you didn't even try :/.

    const myAsyncThing = async (init) => await task2(await task1(init))
This makes the promise chain look much more like what it is, given that the rest of the language uses nesting calls to indicate "after".

mrozbarry|1 year ago

I didn't write it like that because it obscures the call order. I wasn't trying to compare code line/count, just flow control and readability, which is certainly subjective.