How to decide what promise will then be / catch according to

Everything:

I'm new to Promise, here is an example:

var someAsyncThing = function() { return new Promise(function(resolve, reject) { // this will throw, x does not exist resolve(x + 2); }); }; var someOtherAsyncThing = function() { return new Promise(function(resolve, reject) { reject('something went wrong'); }); }; someAsyncThing().then(function() { return someOtherAsyncThing(); }).catch(function(error) { console.log('oh no', error); }); 

I donโ€™t quite understand how .then () works with Promise, right now I can understand that someAsyncThing() returns a Promise that throws an exception and therefore goes to the .catch() part. And if we change resolve(x+2) to resolve(4) , then it will go on to run someOtherAsyncThing(); and return another promise

The first question is that returning .then () is a promise?

And the second question , 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() just an exception catch caused by someAsyncThing() promise (and the same question for chained .then() , if any)?

thanks

+5
source share
3 answers

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); }); 
+3
source

Bugs bubble in promises until they catch.

If you have the following ( <= indicates contains):

 Promise1 <= Promise2.catch(e) <= Promise3 <= throw 

Then Promise3 throws and rejects. Promise2 rejects and then breaks. Promise1 resolves successfully because it does not receive a denial.

 Promise1 <= Promise2.catch(e) <= Promise3 <= throw ^ resolves ^ rejects ^ rejects 

Think of it as a try / catch async.

 Promise1 = Promise.reject(err); // or throw Promise2 = Promise1.catch(err => { // error caught }); Promise3 = Promise1.catch(err => { throw err; // error not caught but thrown back }); // Now Promise1 is rejected. Promise2 is resolved. Promise3 is rejected. 

You also need to know that each then/catch call creates a new promise. I suggest you read Promises / A + spec and save it as a reference.

+3
source

You can catch() to fulfill an internal promise:

 a().then(function() { return b().catch(function() { console.log('boo'); }); }).catch(function(error) { console.log('oh no', error); }); 

If a() rejected, "oh no" will be registered. If b() rejected, "boo" will be registered, but not "oh no" .

+2
source

Source: https://habr.com/ru/post/1237960/


All Articles