在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 能否解釋一下代碼中Promise執(zhí)行過程

能否解釋一下代碼中Promise執(zhí)行過程

class demo {
  promiseFn() {
    return new Promise((resolve, reject) => {
      resolve(10);
    });
  }
  FnA() {
    return this.promiseFn()
      .then(a => {
        throw a;
      })
      .catch(e => {
        console.log(e);
      });
  }
  FnB() {
    this.FnA().then(a => {
      console.log(a, 11111111111);
    });
  }
}

new demo().FnB();
//10
//undefined 11111111111

為什么FnB的then回執(zhí)行?

回答
編輯回答
巷尾

你的代碼執(zhí)行等價(jià)于下面的方式

class demo {
    promiseFn() {
        return new Promise((resolve, reject) => {
            resolve(10);
        });
    }
    FnB() {
        this.promiseFn().then(a => {
            console.log('a' + a)
            throw a;
        }).catch(e => {
            console.log('e' + e);
            return e
            }).then(a => {
            console.log(a, 11111111111);
        });
    }
}

new demo().FnB();
//輸出
a10
e10
undefined 11111111111

因?yàn)閏atch 捕獲執(zhí)行后返回的也是一個(gè)promise,假如你在catch里 return e的話,最后就會(huì)輸出

a10
e10
10 11111111111
2017年3月2日 13:44
編輯回答
遺莣

因?yàn)镕nA的錯(cuò)誤已經(jīng)處理了

2017年5月11日 03:02