Promise basic usage

Insert picture description here


Insert picture description here


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript">
        // Promise基本使用
        console.log(typeof Promise) //function
        console.dir(Promise);

        var p = new Promise(function(resolve, reject) {
            //这里用于实现异步任务
            setTimeout(function() {
                var flag = true;
                if (flag) {
                    //正常情况
                    resolve('hello');
                } else {
                    //一场情况
                    reject('error');
                }
            }, 100);
        })
        p.then(function(data) {
            //正确输出
            console.log(data);

        }, function(info) {
            //错误输出
            console.log(info);

        })
    </script>
</body>

</html>

Insert picture description here

Published 28 original articles · praised 7 · views 1176

Guess you like

Origin blog.csdn.net/haduwi/article/details/105346719