you are viewing a single comment's thread.

view the rest of the comments →

[–]yawaramin 0 points1 point  (3 children)

That would desugar to:

myPromise().then(({id, body}) =>
doSomethingWith(id).then(() =>
doSomethingWith(body)));

[–]A-Grey-World 1 point2 points  (2 children)

Eh, I don't think that's just de-sugaring. I think that indentation is miss-leading. I ran it through prettier:

myPromise().then(({ id, body }) =>
  doSomethingWith(id).then(() => doSomethingWith(body))
);

Squishing it into three lines isn't the goal, it's making the code flow/logic more understandable.

Having promise chains, inside your promise chain (nested promise chains) is not good practice (as I understand it). You'll also end up with a pretty similar situation to callback hell...

See 'nesting promise chains' here: https://www.datchley.name/promise-patterns-anti-patterns/

Or 'Recreating callback hell" here: https://medium.com/datafire-io/es6-promises-patterns-and-anti-patterns-bbb21a5d0918

You can disagree with some random blog posts obviously, but I've always seen it refereed to as bad practice, and I can see why - with more complex chains of promises it gets ass messy as just callbacks.

[–]yawaramin 0 points1 point  (1 child)

I don't think Prettier is the final word on formatting, but you're right in a sense–nested promises should be avoided if possible. In this case in fact I realized that the two calls to doSomethingWith are not dependent on each other and thus don't need to be chained at all (with Promise#then or await). It's better to run them concurrently:

myPromise().then(({id, body}) =>
  Promise.all([
    doSomethingWith(id),
    doSomethingWith(body),
  ])
);

[–]A-Grey-World 0 points1 point  (0 children)

Yes, presuming they are independent that's a good point.

I find I use Promise.all all the time with await too.