eslint-plugin-promise
eslint-plugin-promise copied to clipboard
Prefer calling `then` and `catch` separately to combining them into a single `then`
Essentially, Promise.prototype.then allows taking in two arguments to respond to both resolved and rejected promises. Essentially, p.then(f, g) is equivalent to p.catch(g).then(f); note that the catch comes first since errors thrown by the then handler are not caught by the catch handler.
It would be nice to automatically replace instances like this since it makes it easier to visually scan for catch to check if an error is handled. It also helps emphasise the fact that the then is not handled by the catch, which isn't as apparent when it's ordered visually after the then handler.
Are you proposing to keep them separate AND to order them. Maybe that would be a config option. I'm not necessarily sold on the ordering thing.
To clarify, I mean that then(f, g) is equivalent to catch(g).then(f) always, so it can be auto-applied. But the exact ordering of then and catch is up to the user. So, if any automatic combining or splitting is done, it has to be done for things in that order, because otherwise it might change the functionality.
then(f, g) is equivalent to catch(g).then(f) always
Actually, no, they are not equivalent.
Promise.resolve()
.then(
() => {throw new Error},
() => console.error('error caught')
).catch(() => {})
Promise.resolve()
.then(() => {throw new Error})
.catch(() => console.error('error caught'))
.catch(() => {})
Sorry, I didn't read carefully, please ignore my comment above.