I'm using Promises in NodeJS project, and would like to understand Promise.chains better.
I have one exposed function:
Main library function, that I want my users to call. It will return a promise, and the users then need to handle the resolve and reject's.
That Main library function will call many other library functions, which I want to chain. So each of those functions will return a promise like so:
mainLibraryFunction = function (arguments...) {
return this.firstLibraryFn().then(secondLibraryFn).then(thirdLibraryFn)...
}
Each of these will return a promise, that can be either reject or resolve. What I would want is that if any of the library functions rejects, then instead passing the reject to the next function, I would like to return the reject out of the MainLibraryFunction. Because otherwise I need to implement the handling of error case for each of the libraryfunctions. Because each of them will get the promise as argument, I would need to check in each of the functions, that did I get resolved or rejected promise as paramater. Then I would need to pass that rejected parameter through all the functions until the last function could then return it out of the MainLibraryFunction. This wouldn't be wise.
So what would be the best practise handling this? Should I add catch in the end of the chain? I heard if I add a catch to the end, it should break the chain even if any of them will reject. Then from the catch, I could return Promise.reject() out of the MainLibraryFunction.
return this.firstLibraryFn().then(...)
- note the parens – Alnitak