I'm having trouble understanding the difference between the 2 approaches taken.
I have a certain Promise.All which works as expected, after that, a then function is used to extract data from the responses. I can't figure out why if I use the forEach approach i get printed the data, but if I use the map approach i get return Promises.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
Promise.all(
["koop4", "gVenturi", "aledocdonnini" ].map( user => fetch( 'https://api.github.com/users/'+user+'/repos', { method: 'GET'}) )
).then( res => {
// this works properly
const data = [];
res.forEach( async r => {
data.push(await r.json());
});
return data;
// this return promises
/* return res.map( async r => await r.json() n)*/
}).then(console.log)
</script>
</head>
<body>
</body>
</html>
awaitinforEach- it doesn't work like you think - there's a question around here about it, let me find it for you[]when you return it - what you see in the console is a mirage (trust me).then(console.log)to.then((results => console.log(JSON.stringify(results))))and you'll see that you are NOT getting the results when you think you are - the console lies :p it shows the content of objects evaluated when you look at it, not when it is logged[]... not what the actual console shows, is it