I guess for...of loop worked because you use async on the parent function, of which console.log(newArr) was part of or was present within that async function, which is not happening in case of a forEach loop. It is not an inherent behavior of for...of loop but the way you wrote code.
I've wasted all day hanging around this, even though I had the correct async await syntax. It was finally because of the fucking foreach. Thank you man!
Thank you @Will Ward! My server uses python asyncio and websockets and it has a loop what is collecting the "online" status from the column of "usernames" from the database , push to frontend in json format, similar with your initial array. Every time when the front end got an update it had not updated the list just appended, but the console log worked fine. After I learned from you how to use the foreach properly with asynchronous updated. my "sidebar list" with " usernames" works as expected. Keep up the good work!
hey I have a question let's say all this code is inside an async function and I want to return the newArr (in your case you are logging it), Whenever I call my async function newArr should be returned instead of logging it how to do that?
EXCELLENT! thank you so much! i was trying to get axios request in some order of an array an this is the solution :) ill leave my code here for anyone that is looking for the same: const members = [{"id":1,"name":"jhon"}] //this is an example async function get_from_axios(){ for (const member of members) { let response = await axios.get(axios.defaults.baseURL+'/url?query='+member.id) //return response await this.another(response.data);//must return a promise to wait another() } } async function another(data){ return new Promise((resolve)=>{ setTimeout(() => { resolve(console.log(data)); }, 1500); }) } if it didnt work is because i added the 'function' word because i pulled out from methods in vue and tryed to share with you :) im so glad for have find this video greetings from Argentina