Per this fiddle, if I try to call a second promise function from the .finally() block of the first promise, the second promise resolves but the resolve value is “undefined”.
However, if I call the second promise function in a .then block after the .finally block, everything works as expected. Why can’t I call the second promise in the .finally block of the first promise?
Example that fails (Promise 2 value is “undefined”): https://jsfiddle.net/scemohkb/
function res(v) { return new Promise((resolve, reject) => { resolve(v); }) }
function rej(v) { return new Promise((resolve, reject) => { reject(v); }) }
rej(1)
.then((val) => {
console.log(`Value1: ${val}`);
})
.catch((e) => {
console.error(`Caught 1: ${e}`);
})
.finally(() => {
console.log("Finally 1");
return res(2);
})
.then((v) => {
console.log(`Value2: ${v}`); // THIS DOESN'T WORK: v is undefined
})
.catch((e) => {
console.log(`Caught 2: ${e}`);
})
.finally(() => {
console.log("Finally 2");
});
Example that works: (Promise 2 returns the correct value): https://jsfiddle.net/scemohkb/1/
function res(v) { return new Promise((resolve, reject) => { resolve(v); }) }
function rej(v) { return new Promise((resolve, reject) => { reject(v); }) }
rej(1)
.then((val) => {
console.log(`Value1: ${val}`);
})
.catch((e) => {
console.error(`Caught 1: ${e}`);
})
.finally(() => {
console.log("Finally 1");
})
.then(() => {
return res(2);
})
.then((v) => {
console.log(`Value2: ${v}`); // This works: v is the correct value.
})
.catch((e) => {
console.log(`Caught 2: ${e}`);
})
.finally(() => {
console.log("Finally 2");
});
In example 1, the second promise function still returns correctly upon calling resolve() and the .then block is executed, but the value sent to resolve() is ignored and instead the .then() function receives “undefined”.
What is different about the .finally() function that I’m missing, that prevents this from happening? It would seem to make sense for the cleanest chaining of multiple promises that can resolve or reject independently, vs. adding another .then() function after the .finally().