javascript - fetch res.json continues to return unresolved promise -


i'm expecting this:

fetch('https://response.democratsabroad.org/photos/?tag=alex')     .then(res=>res.json())     .then(res=>console.log(res)) 

to return json object. instead returning pending promise.

promise {[[promisestatus]]: "pending", [[promisevalue]]: undefined} 

screenshot of javascript console why passing unresolved promise instead of resolved value?

first off, in case did not know already, anytime run line of code directly in console, console print out whatever line of code evaluates to. if line of code function call or series of functions calls in fetch(...).then(...).then(...), console show chain of functions returns. doesn't wait promises resolve or callbacks called, executes line of code , reports returned.

and, fetch().then() or fetch().then().then() returns promise in pending state. that's does. then, sometime later when fetch() operation done first promise complete , call .then() handlers.

but, if you're looking @ fetch().then() or fetch().then().then() returns in console, it's going returning unresolved promise in pending state because async operation has not yet completed that's return value console show.

you use promises execute .then() handlers @ appropriate time in future. so, if want see result of async operations, should logging passed .then() handlers , when called, see appropriate data logged.

note: should have .catch() handler @ end of chain able see errors happen in promise chain.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -