学习链接

网道教程:Promise

Promise.prototype.finally

Promise.prototype.finally - 图1

  1. promise
  2. .finally(() => {
  3. // 语句
  4. });
  5. // 等同于
  6. promise
  7. .then(
  8. result => {
  9. // 语句
  10. return result;
  11. },
  12. error => {
  13. // 语句
  14. throw error;
  15. }
  16. );

实现

  1. Promise.prototype.myFinally = (callback) => {
  2. return this.then(
  3. value => Promise.resolve(callback()).then(() => value),
  4. reason => Promise.resolve(callback()).then(() => { throw reason; })
  5. )
  6. };