The first question is that returning .then () is that promise?
p.then() returns a promise - always. If the callback passed to the .then() handler returns a value, then the newly received promise will be resolved simultaneously with the parent's promise, but will be resolved with the returned value from the handler.
If the .then() handler returns a promise, then the promise returned by .then() is not fulfilled / rejected until the promise returned by the handler is filled or rejected. They are connected together.
If the .then() handler throws, then the promise returned by .then() is equally rejected with the exception as a value. A throw is the same as a return of a rejected promise.
if in someOtherAsyncThing() Promise, I use resolve(x+2) to raise an exception, then it will also go to the same part of .catch() , and then how can I make .catch() only catch exception caused by someAsyncThing() promise (same question for chained .then() if any)?
The beauty of promises is that errors propagate through the chain until they are processed. The way you stop the spread of errors at any level is to handle the error there.
If you want to split the possibilities of .catch() , you just need to catch it at a lower level. For instance:
someAsyncThing().then(function() { return someOtherAsyncThing().catch(function(err) { // someOtherAsyncThing rejected here // you can handle that rejection here and decide how you want to proceed // for example, suppose you just want to handle the rejection, log it // and then continue on as if there was no problem, you can just return // a value here return 0; }); }).catch(function(error) { console.log('oh no', error); });
source share