Javascript: Promise understanding
1. Return value of .then() function (.catch() is exactly same)
Let's check this code
var promise1 = new Promise(function(resolve, reject) {
// code here
});
var promise2 = promise1.then(param1); // promise2 is a promise
If param1 is not a function, promise2 = promise1.If param1 is a function but promise1 is rejected (param1 is not called), promise2 = promise1.
If param1 is a function and promise1 is resolved (param1 is called), promise2 is new promise and state as below:
var promise1 = new Promise(function(resolve, reject) {
// code here
});
var promise2 = promise1.then(function param1() {
// code here
return return1;
});
If param1 has exception, promise2 is rejected.If param1 has no exception and return1 is not a promise, promise2 is resolved.
If param1 has no exception and return1 is a promise, promise2 = return1.
Note: when .then() have two parampromise1.then(param1, param2).
If promise1 is resolved, return value same aspromise1.then(param1)
If promise1 is rejected, return value same aspromise1.then(null, param2)and same aspromise1.catch(param2)
2. Params
From above code, if we call promise2.then():
promise2.then(function(param3) {
// code here
});
If promise2 is resolved and return1 is not a promise, param3 = return1.If promise2 is resolved and return1 is a promise, param3 is whatever pass on resolve() function inside return1.