function myPromiseAll(arr) {
let result = []
let count = 1
return new Promise((resolve, reject) => {
for (let i = 0; i < arr.length; i++) {
Promise.resolve(arr[i]).then(res => {
// console.log(i)
result[i] = res
// console.log(res)
if (arr.length == res.length ) {
resolve (result)
}
}, reject)
}
})
}
// let p1 = Promise.resolve(3);
// let p2 = 1337;
// let p3 = new Promise((resolve, reject) => {
// setTimeout(resolve, 100, 'foo');
// });
// myPromiseAll([p1, p2, p3]).then(values => {
// console.log(values); // [3, 1337, "foo"]
// })
let p1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'one');
});
let p2 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'two');
});
let p3 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, 'three');
});
let p4 = new Promise((resolve, reject) => {
setTimeout(resolve, 4000, 'four');
});
let p5 = new Promise((resolve, reject) => {
reject('reject');
});
myPromiseAll([p1, p2, p3, p4, p5]).then(values => {
// console.log(values);
}, reason => {
console.log(reason);// reject
});
promise all的实现
转载上一篇:JS各种错误类型
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
js--promise-all
js--promise-all
js+css js--promise-all -
Promise以及Promise.all的简单用法
当你请求多个数据,相统一处理的时候,假设有两个单独的接口获取页面数据,两接口使用Promis。...
java 前端 servlet 数据 json -
Promise thenable All In One
Promise thenable All In One
then thenable Promise js javascript