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.race #32

Open
sihai00 opened this issue Aug 2, 2019 · 0 comments
Open

手写Promise.race #32

sihai00 opened this issue Aug 2, 2019 · 0 comments
Assignees

Comments

@sihai00
Copy link
Owner

sihai00 commented Aug 2, 2019

手写Promise.race

Promise.race方法同样是将多个 Promise 实例,包装成一个新的 Promise 实例

const p = Promise.race([p1, p2, p3]);

上面代码中,只要p1、p2、p3之中有一个实例率先改变状态,p的状态就跟着改变

实现

Promise.race = promises => new Promise((resolve, reject) => {
	promises.forEach(promise => {
		promise.then(resolve, reject)
	})
})

参考

ECMAScript 6 入门 - 阮一峰

@sihai00 sihai00 self-assigned this Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant