problem description
A loop within function in es6. There is asynchronism in the loop. How to make the final result be return, outside the loop return is not the desired result
the environmental background of the problems and what methods you have tried
for web projects developed with vue2, both Promise and async/await have a problem with getting promise objects within the loop, and cannot get the desired results from return outside the loop
related codes
/ / Please paste the code text below (do not replace the code with pictures)
async _ normalizeSongs (list) {
if(!list) {
return
}
let rest = []
let index = 1
console.log(list)
for(let i=0;i<list.length;iPP) {
if(list[i].songid && list[i].albummid) {
let res = await getSongVkey(list[i].songmid)
if(res.code === ERR_OK) {
const filename = res.req_0.data.midurlinfo[0].filename
const vkey = res.req_0.data.midurlinfo[0].vkey
const newSong = createSong(list[i], filename, vkey)
// console.log(newSong)
rest.push(newSong)
}
}
indexPP
}
return rest
}
_genResult(data) {
let rest = []
if(data.zhida && data.zhida.singername) {
rest.push({...data.zhida,...{type: TYPE_SINGER}})
}
if(data.song) {
let promise = this._normalizeSongs(data.song.list)
promise.then((res) => {
console.log(res)
/* rest = rest.concat(res)
this.result = rest*/
rest = rest.concat(this._normalizeSongs(data.song.list))
})
}
console.log(rest)
return rest
}
what result do you expect? What is the error message actually seen?
_ genResult in this function, how does the res in promise return this function? the passers-by show it to you. Thank you very much
.