eslint-plugin-promise icon indicating copy to clipboard operation
eslint-plugin-promise copied to clipboard

Prefer calling `then` and `catch` separately to combining them into a single `then`

Open ghost opened this issue 4 years ago • 4 comments

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.

ghost avatar Aug 04 '21 19:08 ghost

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.

xjamundx avatar Oct 21 '21 05:10 xjamundx

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.

ghost avatar Oct 27 '21 13:10 ghost

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(() => {})

fisker avatar Nov 08 '21 03:11 fisker

Sorry, I didn't read carefully, please ignore my comment above.

fisker avatar Nov 08 '21 03:11 fisker