簡體   English   中英

通過ES6 Katas,擴展承諾

[英]Working through ES6 Katas, extending 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());
});

});

並收到此錯誤:

“構造函數承諾需要'新'”

我正在使用'新',所以它對我有什么要求?

這似乎是答案

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