简体   繁体   English

通过ES6 Katas,扩展承诺

[英]Working through ES6 Katas, extending promises

I'm trying to extend these two promises: 我正在努力扩展这两个承诺:

   it('using `class X extends Promise{}` is possible', function() {
  class MyPromise extends Promise {
    constructor()
    {
      super();
    }
  };
  const mypromise = new MyPromise((resolve) => resolve());

  promise
    .then(() => done())
    .catch(e => done(new Error('Expected to resolve, but failed with: ' + e)));
});

it('must call `super()` in the constructor if it wants to inherit/specialize the behavior', function() {
  class ResolvingPromise extends Promise {
    constructor() {
    super();
    }}

  return new ResolvingPromise((resolve) => resolve());
});

});

and am receiving this error: 并收到此错误:

"Constructor Promise requires 'new'" “构造函数承诺需要'新'”

I'm using 'new', so what does it want from me? 我正在使用'新',所以它对我有什么要求?

This seems to be the answer 这似乎是答案

it('must call `super()` in the constructor if it wants to inherit/specialize the behavior', function() {
      class ResolvingPromise extends Promise {
        constructor(resolve) {
          super(resolve);
        }
      }

      return new ResolvingPromise((resolve) => resolve());
    });
it('using `class X extends Promise{}` is possible', function() {
      class MyPromise extends Promise {}
      const promise = new MyPromise((resolve, reject) => {})

      promise
        .then(() => done())
        .catch(e => done(new Error('Expected to resolve, but failed with: ' + e)));
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM