3

I'm using ESLint and I get this error: Each then() should return a value or throw promise/always-return

Code:

return somePromise.then(result => {
    console.log(result);
}).catch(error => {
    console.error(error);
});

Why should I return from this promise? There is clearly no need for it because I only wanted to print the result in the log and that's it. This rule seems wrong to apply in all cases. I'm writing a Firebase database trigger which I believe is only interested in knowing whether the promise has resolved or not.

skynew
  • 380
  • 3
  • 13
  • Possible duplicate of https://stackoverflow.com/questions/48750847/each-then-should-return-a-value-or-throw-when-using-promises – Şivā SankĂr Jan 19 '19 at 07:07
  • 1
    If you don't consider the rule useful, disable it. But IMO it's better to always return a value, otherwise consumers of the promise will see `undefined` as the argument, which may well be confusing. – CertainPerformance Jan 19 '19 at 07:13
  • 1
    I'd argue that you're doing two things that are conflicting: 1) returning the promise and 2) not returning the value. If you're going to return the promise, then return its value and rethrow in the `.catch()`. If you don't care to pass the value, then you probably don't need to return the promise either. – jfriend00 Jan 19 '19 at 07:31
  • I need to return the promise because Firebase Functions need to know that my asynchronous task has finished. Firebase does not have any use for what's in the resolved value. That's why I didn't return anything from the then() function. – skynew Jan 19 '19 at 08:45
  • @harsh989 Does Firebase also not need to distinguish between success and error cases? – Bergi Jan 19 '19 at 11:30
  • Btw, I would suggest to [use `.then(…, …)` instead of `.then(…).catch(…)`](https://stackoverflow.com/q/24662289/1048572) for this use case. – Bergi Jan 19 '19 at 11:30
  • @Bergi If I want to use Firebase's built-in retry-on-failure feature, then I can return reject promises directly. But since I am doing all the error handling in my code myself, I avoid using Firebase's retry feature and don't let any errors escape. – skynew Jan 19 '19 at 15:35

1 Answers1

6

This eslint-plugin-promise rule exists to prevent mistakes caused by promises not being chained:

return somePromise.then(result => {
   anotherPromise.then(...);
});

If you believe you cannot benefit from this rule and it only makes your code cluttered with extra return statements or eslint-disable-* comments, disable it.

Bluebird is known for preventing such mistakes, it produces a warning in case there's nested unchained promise.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • @NikKyriakides The library keeps a list of other Bluebird promises that were created at the time when `then` callback was called, then it checks against this list and checks if a callback returned something. This will only work if all involved promises are Bluebird. – Estus Flask Jan 19 '19 at 11:01