通过ES6 Katas工作,扩展承诺

Working through ES6 Katas, extending promises

本文关键字:扩展 承诺 工作 ES6 Katas 通过      更新时间:2023-09-26

我试图扩展这两个承诺:

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

和我收到这个错误:

"Constructor Promise require 'new'"

我用的是new,那么它想从我这里得到什么呢?

这似乎是答案

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