Hacker News new | past | comments | ask | show | jobs | submit login

No need to nest promises:

  console.log('start')
  doA()
    .then(() => doB())
    .then(() => doC())
    .then(() => console.log('finish'))
    .catch((error) => {
      console.log(error)
    })



Is this not less typing and as clear?

  console.log('start')
  doA()
    .then(doB)
    .then(doC)
    .then(() => console.log('finish'))
    .catch((error) => {
      console.log(error)
    })


It's been my experience that doing it that way can fiddle with the `this` value of those B and C functions, although I do in general agree there's usually no need for the outer wrapping arrow function if the interior one doesn't care about `this`

I believe your .catch can similarly be `.catch(console.log)` for the same reason


i was being a bit facetious. "Real-life" code is usually more complex and nesting is needed when you have conditional logic (call doC or doD depending on the return value of the previous call etc..)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: