首页 > 其他 > 详细

promise两个参数的具体作用

时间:2018-09-01 23:18:11      阅读:221      评论:0      收藏:0      [点我收藏+]

 Promise通常配合then方法来链式的使用,then方法里面第一个回调函数表示成功状态,也就是resolve通过.then调用,第二个是失败状态-reject通过.Cath调用,如果默认写一个参数的话,默认resolve

  代码会输出 Hello World!

通过封装函数实现.then用法

        function Print (ready) {
            return new Promise ((resolve,reject)=>{
                if(ready){
                    resolve("Hello World!");
                }else{
                    reject("Good bye!");
                }
            });
        }
        function print1 () {
            alert("World");
        }
        function print2 () {
            alert("!");
        }
        Print(true)
            .then(message=>{alert(message);})
            .then(print1)
            .then(print2)
复制代码

  通过封装函数实现.cath用法

    function Print (ready) {
            return new Promise ((resolve,reject)=>{
                if(ready){
                    resolve("Hello World!");
                }else{
                    reject("Good bye!");
                }
            });
        }
        function print1 () {
            alert("World");
        }
        function print2 () {
            alert("!");
        }
    function catch_error () {
      alert(‘error‘);
    }
        Print(false)
            .then(message=>{alert(message);})
            .then(print1)
            .then(print2)
        .catch(catch_error)
复制代码

 

promise两个参数的具体作用

原文:https://www.cnblogs.com/zbx-boke/p/9571704.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!