Generally, array traversal is carried out in sequence. If asynchrony is involved in the data, the position of the data in the array is not so important. Usually, redcue and PROMISE can be used to solve the traversal problem of the asynchronous array
There is an array like this:
const make = (n, duration) = > new Promise(() = > setTimeout(() = > console.log(n), duration))
let arr = [make(5.1000), make(7.500), make(2.100), make(1.600), make(8.800)]
Copy the code
Timer length output
arr.forEach(v => v.then(console.log.bind))
=> 2 -> 7 -> 1 -> 8 -> 5
Copy the code
Order of the output
arr.reduce((p, v) = > p.then(() = > v.then(console.log)), Promise.resolve())
5 -> 7 -> 2 -> 1 -> 8
Copy the code