I have this piece of code:
const Axios = require('axios');
const baseURL = 'https://jsonplaceholder.typicode.com';
async function main() {
const posts = await Axios(`${baseURL}/posts`);
const users = await Promise.all(posts.data.map( async (post, index) => {
let d = await Axios(`${baseURL}/users/${post.userId}`);
return d.data.name;
}))
users.map( (user, index) => {
console.log( `${index}. ${user}` );
});
}
And outputs the result in order:
1. Patricia
2. Glenna
3. Nicholas
4. Kurtis
5. Chelsey
Everything Ok, but ... If I do:
async function main() {
const posts = await Axios(`${baseURL}/posts`);
await Promise.all(posts.data.map( async (post, index) => {
let d = await Axios(`${baseURL}/users/${post.userId}`);
console.log( `${index}. ${d.data.name}` );
}))
}
the console.log
inside map
prints the list unordered ...
2. Glenna
4. Kurtis
5. Chelsey
1. Patricia
3. Nicholas
My question is:
Why in the first code map
returns the list ordered, and in the second one the console.log
inside map
, prints the list unordered ?