Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现 Promise.all #879

Open
lgwebdream opened this issue Jul 6, 2020 · 2 comments
Open

实现 Promise.all #879

lgwebdream opened this issue Jul 6, 2020 · 2 comments
Labels
JavaScript teach_tag 头条 company 微医 company 有赞 company 滴滴 company 编程题 teach_tag

Comments

@lgwebdream
Copy link
Owner

Promise.all = function (arr) {
  // 实现代码
};
@lgwebdream lgwebdream added JavaScript teach_tag 头条 company 微医 company 有赞 company 滴滴 company 编程题 teach_tag labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@wuchr
Copy link

wuchr commented Jul 21, 2020

Promise.all = function(promises) {
let results = [];
let promiseCount = 0;
let promisesLength = promises.length;
return new Promise(function(resolve, reject) {
for (let val of promises) {
Promise.resolve(val).then(function(res) {
promiseCount++;
// results.push(res);
results[i] = res;
// 当所有函数都正确执行了,resolve输出所有返回结果。
if (promiseCount === promisesLength) {
return resolve(results);
}
}, function(err) {
return reject(err);
});
}
});
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 头条 company 微医 company 有赞 company 滴滴 company 编程题 teach_tag
Projects
None yet
Development

No branches or pull requests

2 participants