All:
I am pretty 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 do not quite get how .then() works with Promise, right now I can understand the someAsyncThing()
return a Promise, which will generate an exception and thus go to .catch()
part. And if we change resolve(x+2)
to resolve(4)
, then it will go to run someOtherAsyncThing();
and return another promise,
the first question is if the return of .then() is the that Promise?
And second question, if in someOtherAsyncThing()
's Promise, I use resolve(x+2)
to cause an exception, then it will also go to same .catch()
part, then how can I make that .catch()
only catch exception caused by someAsyncThing()
's promise(and same question for chained .then()
if there is any)?
Thanks