If you chain multiple
.then
You should always add areturn
at the end of their respective callbacks
Else they will execute at the same time
That is not true. Try the following code:
function foo () {
return new Promise(function(resolve, reject) {
setTimeout(resolve(“resolved value”), 1000);
});
};
foo().then(function (val) {
debugger;
console.log(first then");
}).then(function (val) {
debugger;
console.log("second then");
});
If you run this example you’ll see that “first then” is printed and only after that “second then” is printed in the console.
Note that, when a function doesn’t have an explicit return
statement it returnsundefined
explicitly. If return
statement, from the then
callback , is not a Promise
it will be wrapped into a Promise
explicitly.
Regarding this part:
“When a
reject
is caught if you have a.then
chained to i
It will still execute that.then
You can see the.then
as an “always executes”
I cannot understand what you’ve meant here.