宽屏模式

Promise 调用的三种方式

方式一:

       new Promise(function (resolve, reject) {
           
        }).then(function (resolve) {
           
        }, function (reject) {
           
        }).finally(function () {
           
        });

方式二:

        new Promise(function (resolve, reject) {

        }).then(function (resolve) {

        }).catch(function (reject) {

        }).finally(function () {

        });
方式三:
       new Promise((resolve, reject) => {
           
        }).then((resolve) => {
          
        }).catch((reject) => {
          
        }).finally(() => {
           
        });

例子01:

   new Promise((resolve, reject) => {
            if (1 > 2) {
                resolve(() => {
                    alert("解决了");
                });
            } else {
                reject("再接再厉");
            }
        }).then((resolve) => {
            resolve();//因为传递来的是一个函数,所以可以这样调用
        }).catch((reject) => {
            alert('结果:' + reject);//因为传递来的是一个字符串,可以拼接
        }).finally(() => {
            alert('我最后运行');
        });

例子02:

        new Promise(function (resolve, reject) {
            if (1 > 2) {
                resolve(function () {
                    alert("解决了");
                });
            } else {
                reject("再接再厉");
            }
        }).then(function (resolve) {
            resolve();//因为传递来的是一个函数,所以可以这样调用
        }).catch(function (reject) {
            alert('结果:' + reject);//因为传递来的是一个字符串,可以拼接
        }).finally(function () {
            alert('我最后运行');
        });

例子03:

        new Promise(function (resolve, reject) {
            if (1 > 2) {
                resolve(function () {
                    alert("success");
                });
            } else {
                reject("再接再厉");
            }
        }).then(function (resolve) {
            resolve();//因为传递来的是一个函数,所以可以这样调用
        }, function (reject) {
            alert('结果:'+reject);//因为传递来的是一个字符串,可以拼接
        }).finally(function () {
            alert('我最后运行');
        });

Larwas
请先登录后发表评论
  • latest comments
  • 总共0条评论