Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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..)




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

Search: