Comments (21)
Promise._all = (iterObj) => {
// 1. iterObj 必须是一个可迭代对象, 否则, 无法正常进行则抛出错误
if(!(typeof iterObj === "object" && iterObj !== null && typeof iterObj[Symbol.iterator] === "function")){
throw new TypeError(`${iterObj} is not iterable`);
}
iterObj = [...iterObj];
/*
* 2. 函数返回值为 `<Promise>` 对象, 当参数 `iterObj` 内所有的 `Promise` 成功,
* 该 `<Promise>` 对象成功, 成功数据为所有成功的 `Promise` 结果数组,
* 有一个不成功, 则该 `<Promise>` 不成功, 失败数据为失败原因字符串
*/
return new Promise((resolve, reject) => {
const len = iterObj.length;
let count = 0;
if(len === 0) return resolve([]);
const res = new Array(len);
iterObj.forEach(async (item, index) => {
const newItem = Promise.resolve(item);
try{
const result = await newItem;
res[index] = result;
if(++count === len){
resolve(res)
}
}catch(err){
reject(err);
}
})
})
}
// 验证:
function test(){
try{
Promise._all(null).then(res=>console.log(res), rej=>console.log(rej));
// throw err: null is not iterable
}catch(e){
console.log(e)
}
try{
Promise._all({}).then(res=>console.log(res), rej=>console.log(rej));
// throw err: [object object] is not iterable
}catch(e){
console.log(e)
}
Promise._all([]).then(res=>console.log(res), rej=>console.log(rej));
// []
Promise._all(new Set()).then(res=>console.log(res), rej=>console.log(rej));
// []
Promise._all(new Map()).then(res=>console.log(res), rej=>console.log(rej));
// []
Promise._all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
4,
]).then(res=>console.log(res), rej=>console.log(rej))
// [1, 2, 3, 4]
Promise._all([
Promise.reject(1),
Promise.resolve(2),
Promise.resolve(3),
4,
]).then(res=>console.log(res), rej=>console.log(rej))
// 1
}
test();
from js-challenges.
/**
* 得到一个新的Promise,该Promise的状态取决于proms的执行
* proms是一个迭代器,包含多个Promise
* 全部Promise成功,返回的Promise才成功,数据为所有Primsise成功的数据,并且顺序时按照传入的顺序排列
* 只要有一个Promise失败,则=返回的Pormise失败,原因是第一个Promise失败的原因
* @param {iterator} proms
*/
Promise.all=function (proms) {
return new MyPromise((resolve, reject) => {
try {
const results = []; //接受每个Promise成功后的结果
let count = 0; // Promse计数总数
let fulfilledCount = 0; // 已完成的数量
for (const p of proms) {
// resolve([1,2,3,4])
// 思考:如何保证数组是有序的?如何知道啥时候所有promise都完成?
let i = count; // 保存一下当前下标
count++;
/**
* 先包一下p,处理有直接写数字的情况
*/
MyPromise.resolve(p).then((data) => {
//异步,代码运行到这里,循环就已经结束了,因为在微队列
fulfilledCount++;
// console.log(i);
results[i] = data;
// results.push(data);不能用push
console.log(fulfilledCount);
if (fulfilledCount === count) {
// 当前是最后一个Promise完成了
console.log("全部完成");
resolve(results);
}
}, reject);
}
console.log(count, fulfilledCount);
/**
* 特殊情况1:传递空数组
*/
if (count === 0) {
resolve(results);
}
} catch (error) {
reject(error);
console.error(error);
}
/**
* 特殊情况2:执行中报错:try,catch
*/
});
}
/*******test all *******/
/**
* 不仅可以传数组,还可以传递迭代器。不是所有迭代器都支持for循环,所以。。。
*/
Promise.all([
MyPromise.reject(1),
MyPromise.resolve(2),
MyPromise.resolve(3),
4,
]).then(
(data) => {
// data:[1,2,3,4]
// 传递[pro1,pro2,pro3,4]的话:内部默认处理Promise.resolve(4)
console.log("成功", data);
},
(reason) => {
// reason:reason2
console.log("失败", reason);
}
);
from js-challenges.
Promise._all = function (promises) {
// 该方法返回一个promise对象
return new Promise((resolve, reject) => {
// 该方法的参数需为一个可迭代对象
if (promises == null || typeof promises[Symbol.iterator] !== "function") {
throw new TypeError(`${promises} is not a iterable`);
}
promises = [...promises];
// 可迭代对象为空则返回已resolve的promise
if (promises.length === 0) {
resolve([]);
}
let count = 0;
const values = [];
promises.forEach((promise, index) => {
// 保证可迭代对象的元素为promise,使用Promise.resolve()处理
Promise.resolve(promise)
.then((res) => {
values[index] = res;
if (++count === promises.length) resolve(values);
})
.catch(reject);
});
});
};
from js-challenges.
Promise.myAll = (promises) => {
return new Promise((resolve, reject) => {
let count = 0
let result = []
const len = promises.length
if (len === 0) {
return resolve([])
}
promises.forEach((p, i) => {
Promise.resolve(p).then((res) => {
count += 1
result[i]=res
if(count === len) {
resolve(result)
}
}).catch(reject)
})
})
}
from js-challenges.
Promise.myAll = (promises) => {
return new Promise((resolve, reject) => {
let idx = 0;
const ans = [];
promises.forEach((item, index) => {
Promise.resolve(item).then(
(res) => {
ans[index] = res;
idx++;
if (idx === promises.length) {
resolve(ans);
}
},
(err) => reject(err)
);
});
});
};
from js-challenges.
Promise._all = function (promiseList) {
return new Promise((resolve, reject) => {
// 如果传入的参数 不是数组
if (!Array.isArray(promiseList)) {
return reject(new Error("传入的参数不是数组!"));
}
const res = [];
let counter = 0; // 设置一个计数器
for (let i = 0; i < promiseList.length; i++) {
Promise.resolve(promiseList[i])
.then((val) => {
counter++;
res[i] = val; // 返回值
if (counter == promiseList.length) {
resolve(res);
}
})
.catch((e) => reject(e));
}
});
};
from js-challenges.
function promiseAll(promises) {
return new Promise(function(resolve, reject) {
if(!Array.isArray(promises)){
throw new TypeError(`argument must be a array`)
}
var resolvedCounter = 0;
var promiseNum = promises.length;
var resolvedResult = [];
for (let i = 0; i < promiseNum; i++) {
Promise.resolve(promises[i]).then(value=>{
resolvedCounter++;
resolvedResult[i] = value;
if (resolvedCounter == promiseNum) {
return resolve(resolvedResult)
}
},error=>{
return reject(error)
})
}
})
}
from js-challenges.
Promise.myAll = promises => {
// 参数需要是一个可迭代对象
if(typeof promises[Symbol.iterator] !== 'function'){
throw(`${promises}不是一个可迭代对象`)
}
promises = [...promises]
const len = promises.length,
result = []; //存放结果
let count = 0 //promise完成的个数
if(len === 0){
// 直接返回一个promise包装的数组
return Promise.resolve([])
}
return new Promise((resolve, reject) => {
promises.forEach((pr, idx) => {
// 确保每一项都是promise
Promise.resolve(pr).then(res => {
result[idx] = res
count++
if(count === len){
return resolve(result)
}
}).catch(e => {
reject(e)
})
})
})
}
from js-challenges.
Promise.myAll = function (iterObj) {
//1. 检查arr是否是可迭代的对象
if (
!(
typeof iterObj === "object" &&
iterObj !== null &&
typeof iterObj[Symbol.iterator] === "function"
)
) {
throw new Error(`${iterObj} is not iterable`);
}
iterObj = [...iterObj];
// 创造一个新的promise
return new Promise((resolve, reject) => {
const results = [];
let count = 0;
let finished = 0;
for (let p of iterObj) {
let index = count;
count++;
// 如果传入的p不是promise直接resolve
// 到这一步时所有的for loop 已经结束
Promise.resolve(p).then(
(res) => {
finished++;
results[index] = res;
if (finished === count) {
resolve(results);
}
},
(rej) => {
reject(rej);
}
);
}
});
};
p1 = Promise.resolve(1);
p2 = new Promise((res, rej) => {
setTimeout(() => {
res(2);
}, 1000);
});
p3 = new Promise((res, rej) => {
setTimeout(() => {
res(3);
});
});
const arr = [];
arr.push(p1);
arr.push(p2);
arr.push(p3);
Promise.myAll(arr).then((res) => {
console.log(res);
});
from js-challenges.
function myPromiseAll(promises) {
// 检查是否是迭代对象
if(typeof promises[Symbol.iterator] !== 'function') {
throw(`传入的参数不是一个可迭代对象`)
}
return new Promise((resolve, reject) => {
const results = [];
let count = 0;
promises.forEach((promise, index) => {
Promise.resolve(promise)
.then(result => {
results[index] = result;
count++;
if (count === promises.length) {
resolve(results);
}
})
.catch(error => {
reject(error);
});
});
});
}
from js-challenges.
class MyPromise {
static all(promise) {
if (
!(
typeof promise === "object" &&
promise !== null &&
typeof promise[Symbol.iterator] === "function"
)
) {
throw new Error(`${promise} is not iterator`);
}
return new Promise((resolve, reject) => {
const length = promise.length;
if (length === 0) {
resolve([]);
}
const results = [];
let idx = 0;
promise.forEach((promise, index) => {
Promise.resolve(promise)
.then((res) => {
idx++;
// 返回值跟传入的promise一一对应
results[index] = res;
// 确保所有promise都执行完成
if (idx === length) {
resolve(results);
}
})
.catch((error) => {
reject(error);
});
});
});
}
}
function test() {
try {
MyPromise.all(1)
.then((res) => console.log(res))
.catch((err) => console.log("reject", err));
} catch (error) {
console.log("catch", error);
}
try {
MyPromise.all({})
.then((res) => console.log(res))
.catch((err) => console.log("reject", err));
} catch (error) {
console.log("catch", error);
}
try {
MyPromise.all([])
.then((res) => console.log("success", res))
.catch((err) => console.log("reject", err));
} catch (error) {
console.log("catch", error);
}
MyPromise.all([Promise.resolve(1), {}])
.then((res) => console.log("success", res))
.catch((err) => console.log("reject", err));
MyPromise.all([Promise.reject("12312321"), Promise.resolve(123)])
.then((res) => console.log("success", res))
.catch((err) => console.log("reject", err));
}
test();
from js-challenges.
Promise.myall = (promises) => {
//判断当前数据能否迭代
if (
!(
typeof promises === "object" &&
promises !== null &&
typeof promises[Symbol.iterator] === "function"
)
) {
throw new TypeError(`${promises} is not iterable`);
}
promises = [...promises];
return new Promise((resolve, reject) => {
const len = promises.length;
if (len === 0) return resolve([]);
const ans = [];
let cnt = 0;
promises.forEach(async (promise) => {
try {
const res = await promise;
ans.push(res);
//当所有promise对象都成功时返回成功的promise对象
if (++cnt === len) resolve(ans);
} catch (error) {
//当出现失败的promise对象时立刻返回错误的原因error
reject(error);
}
});
});
};
//test
try {
Promise.myall({}).then(
(res) => console.log(res),
(rej) => console.log(rej)
); //TypeError: [object Object] is not iterable
} catch (error) {
console.log(error);
}
Promise.myall([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
4,
]).then(
(res) => console.log(res), //[ 1, 2, 3, 4 ]
(rej) => console.log(rej)
);
Promise.myall([
Promise.resolve(1),
Promise.resolve(2),
Promise.reject(3),
4,
]).then(
(res) => console.log(res),
(rej) => console.log(rej) // 3
);
Promise.myall([
Promise.resolve(1),
Promise.resolve(2),
Promise.reject(3).catch((err) => console.log(err)), //3
4,
]).then(
(res) => console.log(res), //[ 1, 2, 4, undefined ]
(rej) => console.log(rej)
);
from js-challenges.
/**
* 得到一个新的Promise,该Promise的状态取决于proms的执行
* proms是一个迭代器,包含多个Promise
* 全部Promise成功,返回的Promise才成功,数据为所有Primsise成功的数据,并且顺序时按照传入的顺序排列
* 只要有一个Promise失败,则=返回的Pormise失败,原因是第一个Promise失败的原因
* @param {iterator} proms
*/
function PromiseAll(proms) {
return new Promise((resolve, reject) => {
let fulfilledCount = 0;
const res = [];
proms.forEach((p, index) => {
p.then((val) => {
fulfilledCount++;
res[index] = val;
if (fulfilledCount == proms.length) resolve(res);
}, reject)
})
})
}
// const p1 = Promise.resolve('p1')
// const p2 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('p2 延时一秒')
// }, 1000)
// })
// const p3 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('p3 延时两秒')
// }, 2000)
// })
// const p4 = Promise.reject('p4 rejected')
// const p5 = new Promise((resolve, reject) => {
// setTimeout(() => {
// reject('p5 rejected 延时1.5秒')
// }, 1500)
// })
//成功
// const res1 = PromiseAll([p1, p2, p3])
// res1.then(val => console.log('res1', val),val => console.error(res1,val))
// 一个失败
// const res2 = PromiseAll([p1, p2, p4])
// res2.then(val => console.log('res2', val),val => console.error(res2,val))
//一个延时失败
// const res3 = PromiseAll([p1, p2, p5])
// res3.then(val => console.log('res3', val), val => console.error('res3', val))
//两个失败
// const res4 = PromiseAll([p1, p4, p5])
// res4.then(val => console.log('res4', val), val => console.error('res4', val));
from js-challenges.
Promise.all = function(promises) {
return new Promise((resolve, reject) => {
let result = [];
let len = promises.length;
if(len === 0) {
resolve(result);
return;
}
const handleData = (data, index) => {
result[index] = data;
// 最后一个 promise 执行完
if(index == len - 1) resolve(result);
}
for(let i = 0; i < len; i++) {
// 为什么不直接 promise[i].then, 因为promise[i]可能不是一个promise
Promise.resolve(promise[i]).then(data => {
handleData(data, i);
}).catch(err => {
reject(err);
})
}
})
}
from js-challenges.
Promise.All = function(props) {
if(props.length === 0) Promise.resolve([])
return new Promise((resolve,reject)=>{
const result = []
for(let i = 0;i<props.length;i++) {
Promise.resolve(props[i]).then((data) =>{
result[i] = data
if (i === props.length-1) {
resolve(result)
}
}, reject)
}
})
}低配版
from js-challenges.
Promise.All = function(props) {
if(props.length === 0) Promise.resolve([])
return new Promise((resolve,reject)=>{
const result = []
for(let i = 0;i<props.length;i++) {
Promise.resolve(props[i]).then((data) =>{
result[i] = data
if (i === props.length-1) {
resolve(result)
}
}, reject)
}
})
}``
from js-challenges.
Promise.All = function (ites) {
let result = [];
// 什么时候结束
let count = 0;
return new Promise((res, rej) => {
ites.forEach((val, i) => {
Promise.resolve(val).then((data) => {
res[i] = val;
count++;
if (count === ites.length) {
res(result);
}
}, rej);
});
});
};
from js-challenges.
Promise.all = function (list) {
const result = []
let count = 0
return new Promise((resolve, reject) => {
list.forEach((item, i) => {
Promise.resolve(item).then(res => {
result[i] = res
count++
}).catch(err => {
reject(err)
})
})
if (count === list.length) {
resolve(result)
}
})
}
from js-challenges.
// promise.all会返回一个新的promise,当promise数组中所有的promise都成功才resolve, 而只要其中一个失败就直接reject失败的结果
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(111);
}, 2000);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(222);
}, 1000);
});
const p3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(333);
}, 3000);
});
Promise.all([p1, p2, p3]).then(
(res) => {
console.log(res);
},
(err) => {
console.log(err);
}
);
Promise._all = function (promises) {
return new Promise((resolve, reject) => {
let results = [];
promises.forEach((promise, index) => {
promise.then(
(res) => {
// 加个索引就可以知道是哪个promise的结果
results.push({ res, index });
if (results.length === promises.length) {
resolve(results);
}
},
(err) => {
reject(err);
}
);
});
});
};
Promise._all([p1, p2, p3]).then(
(res) => {
console.log(res);
},
(err) => {
console.log(err);
}
);
from js-challenges.
Promise.all = function (...args) {
return new Promise((resolve, reject) => {
try {
const result = []
let count = 0
const len = args.length
args.forEach((item, i) => {
Promise.resolve(item).then(res => {
result[i] = res
count++
if (len === count) {
resolve(result)
}
}).catch((err) => {
reject(err)
})
})
} catch (error) {
reject(error)
}
})
}
from js-challenges.
Promise.myAll = function (promises) {
return new Promise((resolve, reject) => {
const result = []
let count = 0
if (promises.length === 0) return resolve([])
promises.forEach((p, index) => {
Promise.resolve(p).then(res => {
result[index] = res
count++
if (count === promises.length) {
resolve(result)
}
}).catch(err => {
reject(err)
})
})
})
}
from js-challenges.
Related Issues (20)
- 正则表达式匹配URL HOT 1
- repeat
- add
- CamelCase
- 洗牌算法 HOT 1
- 工厂模式
- 代理模式 HOT 1
- 装饰器模式 HOT 1
- 手写new关键字 HOT 1
- 实现有序数组生成平衡二叉搜索树,ACM模式(2023美团暑期实习一面手撕)
- 版本号排序(2023美团暑期实习一面) HOT 2
- 将html字符串转为树状结构
- 对象去重 HOT 2
- 【开源自荐】推荐一个每日更新的前端面试题库
- bind
- 我的解决方案
- 【开源自荐】SolidUI 一句话生成任何图形
- 百度2023秋招:把数组排成最小的数 HOT 5
- 实现简易版的useState HOT 1
- 手写Instanceof详细 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-challenges.