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

Custom Promise all and race #10

Open
ZhangCheng-zh opened this issue Aug 5, 2022 · 0 comments
Open

Custom Promise all and race #10

ZhangCheng-zh opened this issue Aug 5, 2022 · 0 comments

Comments

@ZhangCheng-zh
Copy link
Owner

ZhangCheng-zh commented Aug 5, 2022

Promise all

Key point:

  • Input is iterator type
  • if input is not promise type, must output the raw data
  • the return must be promise, can then and catch
  • output resolve must keep the raw order
  • output reject must be the earliest reject
  • if input blank, return []
function promiseAll (promisesList) {
    return new Promise((resolve, reject) => {
        const promisesResult = []
        let count = 0
        const len = promisesList.length
        for (let i = 0; i < len; i++) {
            Promise.resolve(promisesList[i]).then(res => {
                promisesResult[i] = res
                if (++count === len) {
                    resolve(promisesResult)
                }
            }).catch(e => {
                reject(e)
            })
        }
        if (promisesList.length === 0) {
            return resolve([])
        }
    })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant