I’m trying to understand the inner workings of a promise chain. I know that a call to then() on a promise always returns a new promise.
When a promise is returned from within a then() callback, does it replace the promise returned by the then() itself? In other words, the inner promise supersedes the outer promise in the chain.
It would be good to know what this promise chain would actually look like flattened out:
function fetchData() {
console.log("Fetching data...");
return fetch('https://api.example.com/data1')
.then(data1 => {
console.log("Data 1 fetched");
return fetch('https://api.example.com/data2');
})
.then(data2 => {
console.log("Data 2 fetched");
return { data1, data2 };
});
}
Also a side-note, if I were to add a final call to then() in this code that does nothing but log something without returning, the promise that then() creates would be resolved with the data2 value?