js promise 同步異步數據展示問題?

getIanMediumList().then(res => {
   this.mixItems = this.mixItems.concat(res.items)
   console.log('A': this.mixItems)
})
getGreenMediumList().then(res => {
   this.mixItems = this.mixItems.concat(res.items)
   console.log('B': this.mixItems)
})
console.log('C': this.mixItems)

A跟B都可以展示出數據
但是C展示不出來,會是空的
如何等兩個都完成後,讓C取得數據?

阅读 1.4k
1 个回答

有异步了当然就得继续异步……

let p1 = getIanMediumList().then(res => {
   this.mixItems = this.mixItems.concat(res.items)
   console.log('A': this.mixItems)
})
let p2 = getGreenMediumList().then(res => {
   this.mixItems = this.mixItems.concat(res.items)
   console.log('B': this.mixItems)
})

Promise.all([p1, p2]).then(() => {
    console.log('C': this.mixItems)
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题