1、Promise.all()
<!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.all()</title>
</head>
<body>
<script>
const f1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('first');
}, 3000)
});
const f2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('second');
}, 2000)
});
const f3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('third');
}, 1000)
});
// const f4 = Promise.reject('err');
Promise.all([f1, f2, f3]).then(res => {
console.log(res); // ['first', 'second', 'third'] 出现reject,无结果输出
});
</script>
</body>
</html>
2、Promise.allSettled()
<!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.all()</title>
</head>
<body>
<script>
const f1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('first');
}, 3000)
});
const f2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('second');
}, 2000)
});
const f3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('third');
}, 1000)
});
// const f4 = Promise.reject('err');
Promise.all([f1, f2, f3]).then(res => {
console.log(res); // ['first', 'second', 'third'] 出现reject,无结果输出
});
</script>
</body>
</html>
3、Promise.any()
<!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.any()</title>
</head>
<body>
<script>
const f1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('first');
}, 3000)
});
const f2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('second');
}, 2000)
});
const f3 = new Promise((resolve, reject) => {
setTimeout(() => {
reject('third err');
}, 500)
});
const f4 = Promise.reject('err');
const f5 = new Promise((resolve, reject) => {
setTimeout(() => {
reject('five err');
}, 100)
});
Promise.any([f1, f2, f3, f4, f5]).then(res => {
console.log(res); // second 获得resove用时最短得结果不会受reject影响
});
</script>
</body>
</html>
4、Promise.race()
<!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 f1 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('first');
}, 1000)
});
const f2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('second');
}, 500);
});
const f3 = Promise.reject('err');
const f4 = Promise.resolve('fourth');
Promise.race([f1, f2, f3, f4]).then(res => {
console.log(res); // second first 获得reject之前得结果,用时短的结果先输出
});
</script>
</body>
</html>
5、Promise.resolve()
<!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.resolve()</title>
</head>
<body>
<script>
const promise = Promise.resolve('success');
promise.then(res => {
console.log(res);
});
</script>
</body>
</html>
6、Promise.reject()
<!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.reject()</title>
</head>
<body>
<script>
const promise = Promise.reject('err');
promise.catch(res => {
console.log(res);
});
</script>
</body>
</html>
7、Promise.prototype.then() Promise.prototype.catch() Promise.prototype.finally()
<!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.prototype.then()、Promise.prototype.catch()、Promise.prototype.finally()</title>
</head>
<body>
<script>
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
// reject('err');
}, 300);
});
myPromise
.then(res => {
console.log(res)
}, err => {
console.log(err)
})
.catch(err => {
console.log(err)
})
.finally(() => {
console.log('finally')
})
</script>
</body>
</html>