We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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.allSettled()
Promise.allSettled()方法返回一个在所有给定的 promise 都已经 fulfilled 或 rejected 后的 promise,并带有一个对象数组,每个对象表示对应的 promise 结果。
promise
fulfilled
rejected
和 Promise.all() 不同,Promise.allSettled() 会等待所有的 promise 直到 fulfill 或者 reject。
Promise.all()
fulfill
reject
The text was updated successfully, but these errors were encountered:
function allSettled(promises) { // 如果传入的参数为空,那么返回一个空的 Promise if (promises.length === 0) { return Promise.resolve([]) } // 匹配传入的参数,如果是 Promise 对象,那么就返回 Promise 对象 let promisesIterable = promises.map((item)=>{ return item instanceof Promise ? item : Promise.resolve(item) }) // 创建一个数组,用来存储所有的 Promise 的状态 let results = [] // 用来记录当前已经完成的 Promise 的数量 let completed = 0 return new Promise((resolve, rejected) => { // 循环遍历传入的参数,并且创建一个 Promise for (let i=0; i < promisesIterable.length; i++) { Promise.resolve(promisesIterable[i]) .then((value)=>{ results[i] = {status: 'fulfilled', value} }) .catch((reason)=>{ results[i] = {status: 'rejected', reason} }) .finally(()=>{ // 当所有的 Promise 都完成的时候,返回结果 并且调用 resolve completed++ if (completed === promisesIterable.length) { resolve(results) } }) } }) }
Sorry, something went wrong.
No branches or pull requests
题目描述
Promise.allSettled()
方法返回一个在所有给定的promise
都已经fulfilled
或rejected
后的promise
,并带有一个对象数组,每个对象表示对应的promise
结果。和
Promise.all()
不同,Promise.allSettled()
会等待所有的promise
直到fulfill
或者reject
。The text was updated successfully, but these errors were encountered: