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.
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)
})