Js performs asynchronous operations in a loop and then adds each asynchronous result to an array. How can this array be used outside the loop?

How does the

code return only the result after the last asynchronous operation in the for loop is completed as shown in the figure? Please stop by and give me a hand.


I know how to solve it, as shown in the code above. It is guaranteed that at the end of the loop, the results of all asynchronous operations saved in the array are obtained outside the loop. Of course, this is not necessarily the best solution. You are welcome to discuss and correct it.


it would be nice to send the array directly in resolve.

let promise = new Promise(resolve => {
    let arr = []
    // ...
    resolve(arr)
})
promise.then(res => {
    console.log(res)
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2b4a6-2b1fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2b4a6-2b1fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?