这样的一个需求,下面
export const productObj = [
{
img: 'img1',
data: [
{
id: 1,
text: '1'
}, {
id: 2,
text: '2'
}, {
id: 3,
text: '3'
}
]
}, {
img: 'img2',
data: [
{
id: 4,
text: '4'
}, {
id: 10042,
text: '5'
}, {
id: 5,
text: '6'
}
]
}
]
根据id请求数据 并把结果放在data数组中对应的对象中。
方法一:
// function onLoad() {
// const infoList = _.cloneDeep(this.productObj)
// const promiseArray = []
// for (const i in infoList) {
// promiseArray.push(this.test(infoList[i].data))
// }
// Promise.all(promiseArray).then(res => {
// console.log(res)
// })
// }
// function test(param) {
// return new Promise((resolve, reject) => {
// const promiseArray =
// []
// for (const
// i in param) {
// promiseArray.push(this.test2(param[i].id))
// }
// Promise.all(promiseArray).then(res => {
// resolve(res)
// })
// })
// }
// function test2(param) {
// return new Promise(async(resolve, reject) => {
// const res = await getPlatDetail(param)
// resolve(res.data)
// })
// }
方法二(简化后):
async promiseAllInfo() {
const infoList = _.cloneDeep(this.productObj)
const info = await Promise.all(
infoList.map(item => {
return new Promise(async resolve => {
const result = await Promise.all(item.data.map(el => {
return new Promise(resolve => {
const result = this.getPlatDetail(el.id)
resolve(result)
})
}))
resolve(result)
})
})
)
this.infoList = infoList
}