Why a different result in the following cases? The first example works correctly, returns an array of three elements ["qwe", "rty", "asd"]
. Second example return only last element ["asd"]
. Please, explain how it work? Why is this behavior happening?
In the first example working through intermediate variable awaitResult
.
class XXX {
constructor() {
this.storage = {1: ['qwe'], 2: ['rty'], 3: ['asd']}
}
async getValue(key) {
return this.storage[key];
}
async logValues() {
let keys = [1, 2, 3]
let values = []
// ----- First version -----
await Promise.all(
keys.map(
async key => {
let awaitResult = await this.getValue(key)
values = values.concat(awaitResult)
}
)
);
console.log(values)
}
}
let xxx = new XXX()
xxx.logValues()
See Question&Answers more detail:os