var tastA = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('成功A')
}, 5000)
})
var tastB = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('成功B')
}, 7000)
})
var tastC = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('成功C')
}, 6000)
})
var total = Promise.all([tastA, tastB, tastC])
window.onload = () => {
total.then(res => {
console.log(total)
}).catch(err => {
console.log(err)
})
}
长话短说,有时候我们需要同时发几个请求但是要保证他们都是返回才可以继续下一步的操作,所以我们可以用
Promise.all 这个属性
大家可以复制上面的代码在本地跑一下
你会发现,耗时只需要7000ms