通过ES6 Katas,扩展承诺

时间:2016-11-03 14:24:50

标签: javascript ecmascript-6

我试图延伸这两个承诺:

   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());
});

});

并且收到此错误:

"构造函数承诺需要' new'"

我正在使用' new',那么它对我有什么要求?

2 个答案:

答案 0 :(得分:2)

这似乎就是答案

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());
    });

答案 1 :(得分:1)

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)));
    });