亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Promise then 方法返回值問題。

Promise then 方法返回值問題。

繁花如伊 2018-08-07 10:09:40
function double(value) {    return value * 2;}function increment(value) {  return new Promise(function(resolve, reject){        resolve(value+9);  })}function output(value) {    console.log(value);// => (1 + 9) * 2}var promise = Promise.resolve(1);promise    .then(increment)    .then(double)    .then(output)    .catch(function(error){        console.error(error);    });    定義: Promise then 返回一個新的 promise。其實上 double方法 返回一個 int類型,increment 返回一個Promise對象double 返回類型與定義不符合,這其中Promise做了什么?是 return new Promise(function(resolve,reject){resolve(value*2)})?那Promise 如何區別return int,string,Promise 做一個層封裝?
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

看看A+Promise

this.then = function (onFulfilled, onRejected) {

  var self = this;

  return new Promise(function (resolve, reject) {

    return self.done(function (result) {

      if (typeof onFulfilled === 'function') {

        try {

          return resolve(onFulfilled(result));

        } catch (ex) {

          return reject(ex);

        }

      } else {

        return resolve(result);

      }

    }, function (error) {

      if (typeof onRejected === 'function') {

        try {

          return resolve(onRejected(error));

        } catch (ex) {

          return reject(ex);

        }

      } else {

        return reject(error);

      }

    });

  });

}

then方法的實現里,確實是返回了一個新的Promise,你說的double,其實是then的第一個參數onFulfilled


查看完整回答
反對 回復 2018-09-24
  • 1 回答
  • 0 關注
  • 5675 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號