2

What are the cons and pros of implementing a promise function/method in the following way? -

Return a promise only if all input parameters pass validation. Otherwise, throw TypeError.


I used both of the following approaches in the past...

  • Simply throwing an error: throw new TypeError('Argument "bla-bla" is invalid...')
  • Returning a rejection: return Promise.reject(new TypeError('Argument "bla-bla" is invalid...'))

Now I am refactoring a library for that, to make it more consistent, and want to know the good/bad sides of either approaches first.

P.S. I have added label bluebird to this question, even though it is not directly related to it, but because the bluebird community tends to have the best expertise in such matters.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • @Bergi, thank you for the links. The second one does provide a good answer to this. But if someone still wants to offer a more specific answer for the input validation here, it'll be fine also :) – vitaly-t Feb 25 '18 at 21:41
  • 1
    [This answer by Patrick Roberts](https://stackoverflow.com/a/45490952/1048572) provides some discussion of distinguishing operational vs programmer errors, but as you can see from the downvotes on it it's very controversial. The community-accepted best practice is to always return a promise, just like an `async function` would. – Bergi Feb 25 '18 at 21:48

0 Answers0