4 ms·
No need to nest promises: console.log('start') doA() .then(() => doB()) .then(() => doC()) .then(() => console.log('finish')) .catch((error
by leipert 5y ago
No need to nest promises:
console.log('start')
doA()
.then(() => doB())
.then(() => doC())
.then(() => console.log('finish'))
.catch((error) => {
console.log(error)
})
- Raed667 5y agoi 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..)
- BeefWellington 5y agoIs this not less typing and as clear? console.log('start') doA() .then(doB) .then(doC) .then(() => console.log('finish')) .catch((error) => { console.log(error) })
- mdaniel 5y agoIt'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