ruanyf / es6tutorial

《ECMAScript 6入门》是一本开源的 JavaScript 语言教程,全面介绍 ECMAScript 6 新增的语法特性。

Home Page:http://es6.ruanyifeng.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Promise 对象一章关于 finally 的实现的问题

bsc2xp opened this issue · comments

commented

文中对于 finally 的实现给出了示例:

Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value  => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  );
};

这里为什么要使用 P.resolve(callback())?写成下面不行吗?

return this.then(
value => {try {callback();} catch(err){}; return value},
reason => {try {callback();} catch(err){}; throw reason }
);

此处不答疑