Skip to content Skip to sidebar Skip to footer

Why Am I Getting Promise { } When Using Fetch In Promise.all?

This works fine: .... .then(() => fetch(link, { headers: {'Content-Type': 'application/json; charset=utf-8'} })) .then( res => res.json()) .then((response)=>{ con

Solution 1:

You have to use .then() on res[1].json() because it just returns a promise and you aren't waiting on that promise anywhere.

What I would suggest is that you change to this:

let call = fetch(link, { headers: {"Content-Type": "application/json; charset=utf-8"} })
    .then(response => response.json());

Then, your call variable has already made the .json() call and Promise.all() will wait on it for you.


let dbconnect = require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", { useNewUrlParser: true } ),
    call = fetch(link, { headers: {"Content-Type": "application/json; charset=utf-8"} })
              .then(response => response.json());

Promise.all( [dbconnect, call] ).then( res => {
    console.log(res[0]);
    console.log(res[1]);
});

Post a Comment for "Why Am I Getting Promise { } When Using Fetch In Promise.all?"