es7.promise.finally.js 763 B

1234567891011121314151617181920
  1. // https://github.com/tc39/proposal-promise-finally
  2. 'use strict';
  3. var $export = require('./_export');
  4. var core = require('./_core');
  5. var global = require('./_global');
  6. var speciesConstructor = require('./_species-constructor');
  7. var promiseResolve = require('./_promise-resolve');
  8. $export($export.P + $export.R, 'Promise', { 'finally': function (onFinally) {
  9. var C = speciesConstructor(this, core.Promise || global.Promise);
  10. var isFunction = typeof onFinally == 'function';
  11. return this.then(
  12. isFunction ? function (x) {
  13. return promiseResolve(C, onFinally()).then(function () { return x; });
  14. } : onFinally,
  15. isFunction ? function (e) {
  16. return promiseResolve(C, onFinally()).then(function () { throw e; });
  17. } : onFinally
  18. );
  19. } });