Promise API

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值