Train_2:promise简例

来源:互联网 发布:matlab软件做量化投资 编辑:程序博客网 时间:2024/05/15 23:55
<body>    <script type="text/javascript">        function eat() {            return new Promise(function(next, fail) {                setTimeout(function() {                    alert('吃完饭了') ;                    next() ;                }, 3000);            }) ;        }        function rest() {            return new Promise(function(next, fail) {                setTimeout(function() {                    alert('休息好了') ;                    // next() ;                     fail('突然想喝点可乐了') ;                }, 3000) ;            }) ;        }        function run() {            return new Promise(function(next, fail) {                setTimeout(function() {                    alert('跑完步了') ;                    next();                }, 3000);            }) ;        }        eat().then(function() {            return rest();        }).then(function() {            return run();        }).then(function() {            alert('今天一天结束了');        }).catch(function(err) {            alert(err);        });    </script></body>
原创粉丝点击