<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>promise.race</title>
</head>
<body>
<script>
const sleep = seconds => new Promise(resolve => setTimeout(() => resolve(seconds), seconds));
</script>
<script>
function promiseRace(_promises) {
return new Promise((resolve, reject) => {
const promises = [..._promises];
const arr = [];
for (let i = 0; i < promises.length; i++) {
Promise.resolve(promises[i])
.then(data => {
resolve(data);
})
.catch(err => {
reject(err);
});
}
});
}
promiseRace([1, 2, 3])
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
// all 一个任务失败全是失败,所有成功才算成功
// any 一个成功就成功,所有失败才失败
// race 返回最先完成的那个任务
// allsettled 返回所有任务完成的状态和值
</script>
</body>
</html>