es6-wrapped-promise.js 986 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. module.exports = (Promise, ensureAslWrapper) => {
  3. // Updates to this class should also be applied to the the ES3 version
  4. // in index.js.
  5. return class WrappedPromise extends Promise {
  6. constructor(executor) {
  7. var context, args;
  8. super(wrappedExecutor);
  9. var promise = this;
  10. try {
  11. executor.apply(context, args);
  12. } catch (err) {
  13. args[1](err);
  14. }
  15. return promise;
  16. function wrappedExecutor(resolve, reject) {
  17. context = this;
  18. args = [wrappedResolve, wrappedReject];
  19. // These wrappers create a function that can be passed a function and an argument to
  20. // call as a continuation from the resolve or reject.
  21. function wrappedResolve(val) {
  22. ensureAslWrapper(promise, false);
  23. return resolve(val);
  24. }
  25. function wrappedReject(val) {
  26. ensureAslWrapper(promise, false);
  27. return reject(val);
  28. }
  29. }
  30. }
  31. }
  32. };