First off, in case you did not know this already, anytime you run a line of code directly in the console, the console will print out whatever that line of code evaluates to. If that line of code is a function call or a series of functions calls as in fetch(...).then(...).then(...)
, then the console will show what that chain of functions returns. It doesn't wait for promises to resolve or callbacks to get called, it just executes that line of code and reports what it returned.
And, fetch().then()
or fetch().then().then()
returns a promise in the pending state. That's what it does. Then, sometime later when the fetch()
operation is done the first promise will complete and it will call its .then()
handlers.
But, if you're just looking at what fetch().then()
or fetch().then().then()
returns in the console, it's always going to be returning an unresolved promise in the pending state because the async operation has not yet completed so that's the return value that the console will show.
You use promises so they will execute their .then()
handlers at the appropriate time in the future. So, if you want to see the result of your async operations, you should be logging what is passed to your .then()
handlers and then when they are called, you will see the appropriate data logged.
Note: you should also have a .catch()
handler at the end of your chain so you will be able to see any errors that happen in your promise chain.