You are completely right, when the article was written I believe my thoughts were of a scenario like:
function foo () {
return new Promise(function(resolve, reject) {
setTimeout(resolve(‘resolved value’), 1000);
});
};foo()
.then(function () {
new Promise(function(resolve, reject) {
setTimeout(resolve(‘resolved value expected on the next then’), 1000);
});
})
.then(function (val) {
console.log(val)
})
Which, like you stated, will return and log undefined
instead of “resolved value expected on the next then”. That can be fixed by doing a return of new Promise(function(resolve, reject))…
on the first .then
Regarding the reject
consider the following:
function foo () {
return new Promise(function(resolve, reject) {
setTimeout(resolve(‘resolved value’), 1000);
});
};foo()
.then(function () {
error.onPurpose
})
.then(function () {
console.log(`won’t execute`)
})
.then(function () {
console.log(`won’t execute also`)
})
.catch(function(error) {
console.log(‘error of not defined’, error)
})
.then(function (val) {
console.log(‘will execute regardless of error or not’);
});
What I meant was that an error is thrown on the first .then
which makes it “skip” the second and third .then
and be caught by the .catch
, regardless of an error being caught the last .then
, and any other that follows, will execute all the same (contrary to what happens with the “skipped” ones that are before the .catch
)
The article will be edited accordingly so not to mislead anyone else, thank you for the feedback.