javascripttypescriptfetchisomorphic-javascriptisomorphic-fetch-api

promise is still pending in then block - how to resolve?


I have code something like -

fetch(`${URL}${PATH}`)
   .then(res => {
       const d = res.json();
       console.log("data is: ", d);

       return d;
    })

It logs data is: Promise { <pending> }.

What to do to see results and utilize in next code statement?

Other questions and answers suggests to use then block to resolve, but I'm still seeing it unresolved.


Solution

  • res.json() is asynchronous. You will need to use an additional .then to get the result.

    fetch(`${URL}${PATH}`)
      .then(res => res.json())
      .then(d => {
        console.log('data is: ', d);
        return d;
      });