JavaScript 中的Promise學習

 

代碼示例:

<script type="text/javascript">
            new Promise(function(resolve, reject) {
                console.log(111);
                resolve(222);
            }).then(function(value) {

                console.log(value);
                return 333;
            }).then(function(value) {
                console.log(value);
                throw "An Error";
            }).catch(function(err) {
                console.log(err);
            }).finally(function(){
                console.log("End");
            });
        </script>

 

示例代碼2:

<html>
    <head>
        <h2> Javascript Promise</h2>
        </br>
    </head>
    <body>
        <script>
            var p = new Promise(function(resolve, reject) {
                var x = 2 + 3;
                if (x == 5)
                    resolve(" executed and resolved successfully");
                else
                    reject("rejected");
            });
            p.then(function(fromResolve) {
                document.write("Promise is" + fromResolve);
            }).catch(function(fromReject) {
                document.write("Promise is " + fromReject);
            });
        </script>
    </body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章