console.log('here we go')
Promise.all([1,2,3])
.then(all=>{
console.log('1: ',all)
return Promise.all([()=>{
console.log('xxoo')
},'xxoo',false])
})
.then(all=>{
console.log('2: ',all)
let p1 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p1`)
}, 1500);
});
let p2 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p2`)
}, 1450);
});
return Promise.all([p1,p2])
})
.then(all=>{
console.log('3: ',all)
let p1 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p1`)
}, 0);
});
let p2 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p2`)
}, 0);
});
let p3 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p3`)
}, 0);
});
return Promise.all([p1,p2,p3])
})
.then(all=>{
console.log('all: ',all)
})
.catch(err=>{
console.log(err)
})
/*
here we go
1: [ 1, 2, 3 ]
2: [ [Function], 'xxoo', false ]
3: [ "I'm p1", "I'm p2" ]
all: [ "I'm p1", "I'm p2", "I'm p3" ]
*/
// Promise.all()
// 批量执行
1.Promise.all([p1,p2,p3,...])用于将多个实例,包装成一个新的promise实例
返回的实例就是普通promise
2.它接受一个数组作为参数
3.数组里面可以是promise对象,也可以是别的值,只有promise会等待状态改变
4.当所有的子promise完成,该promise完成,返回值是全部值的数组
5.有任何一个失败,该promise失败,返回值是第一个失败的子promise的结果
console.log('here we go')
Promise.all([1,2,3])
.then(all=>{
console.log('1: ',all)
return Promise.all([()=>{
console.log('xxoo')
},'xxoo',false])
})
.then(all=>{
console.log('2: ',all)
let p1 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p1`)
}, 1500);
});
let p2 = new Promise(resolve=>{
setTimeout(() => {
resolve(`I'm p2`)
}, 1450);
});
return Promise.all([p1,p2])
})
.then(all=>{
console.log('3: ',all)
let p1 = new Promise((resolve,reject) => {
setTimeout(() => {
resolve(`I'm p1`)
}, 0);
});
let p2 = new Promise((resolve,reject) => {
setTimeout(() => {
reject(`I'm p2 error`)
}, 0);
});
let p3 = new Promise((resolve,reject) => {
setTimeout(() => {
resolve(`I'm p3`)
}, 0);
});
return Promise.all([p1,p2,p3])
})
.then(all=>{
console.log('all: ',all)
})
.catch(err=>{
console.log(err)
})
/*
here we go
1: [ 1, 2, 3 ]
2: [ [Function], 'xxoo', false ]
3: [ "I'm p1", "I'm p2" ]
I'm p2 error
*/