2017年7月17日 星期一

一些 promise 可能會出現的問題

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <script>
        function reject_1() {
            var p = job_1();
        }
        /*------------------------------------*/
        function reject_2() {
            debugger;
            var p = job_1();

            p.then(function() {
                alert('ok');
            }, function() {
                alert('error');
            });

        }
        /*------------------------------------*/
        function reject_3() {
            debugger;
            var p = job_2();

            p.then(function() {
                alert('ok');
            }, function() {
                alert('error');
            });
        }
        /*------------------------------------*/
        function job_1() {
            debugger;
            var _res, _rej;

            var p = new Promise(function(res, rej) {
                _res = res;
                _rej = rej;
            });

            setTimeout(function() {
                _rej();
            });
            return p;
        }
        /*------------------------------------*/
        function job_2() {
            debugger;
            var _res, _rej;

            var p = new Promise(function(res, rej) {
                _res = res;
                _rej = rej;
            });
            _rej();
            return p;
        }
    </script>
</head>

<body>
    <pre>
        function job_1() {
            var _res, _rej;

            var p = new Promise(function(res, rej) {
                _res = res;
                _rej = rej;
            });

            setTimeout(function() {
                _rej();
            });
            return p;
        }

        function reject_1() {
            var p = job_1();
        }

    </pre>
    <button onclick="reject_1();">reject_1</button>
    <hr />
    <pre>
        function job_1() {
            var _res, _rej;

            var p = new Promise(function(res, rej) {
                _res = res;
                _rej = rej;
            });

            setTimeout(function() {
                _rej();
            });
            return p;
        }

        function reject_2() {
            var p = job_1();

            p.then(function() {
                alert('ok');
            }, function() {
                alert('error');
            });

        }

    </pre>
    <button onclick="reject_2();">reject_2</button>
    <hr />
    <pre>
        function job_2() {
            var _res, _rej;

            var p = new Promise(function(res, rej) {
                _res = res;
                _rej = rej;
            });
            _rej();
            return p;
        }

        function reject_3() {
            var p = job_2();

            p.then(function() {
                alert('ok');
            }, function() {
                alert('error');
            });
        }

    </pre>
    <button onclick="reject_3();">reject_3</button>
</body>

</html>