文章 评论 浏览 27
Promise._all = function (promises) { return new Promise((resolve, reject) => { const result = [], resolved = 0 const length = promises.length const addResult = (item, index) => { result[index] = item resolved++ if (resolved === length) { resolve(result) } } promises.forEach((promise, index) => { if (typeof promise.then === 'function') { // thenable promise.then((item) => { addResult(item, index) }).catch(reject) } else { // 非promise类型直接透传 addResult(promise, index) } }) }) }
@gzwgq222 直接用lodash吧
4,2,1
厉害了 大神
文章 0 评论 0
接受
第 80 题:介绍下 Promise.all 使用、原理实现及错误处理