javascript - 通过 ES6 Katas 工作,扩展 promise

标签 javascript ecmascript-6

我正在尝试扩展这两个 promise :

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

关于javascript - 通过 ES6 Katas 工作,扩展 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40404028/

相关文章:

javascript - 在字符串中搜索名称并使用 for 循环记录每次出现的名称

javascript - 当textarea的值改变时是否执行任何事件?

javascript - 功能在需要时触发。我应该如何解决这个问题?

javascript - 将嵌套数组内的对象数组分组并将其总数添加到新数组中

javascript - 在严格模式下更改变量的类型

javascript - 我的图像模式上的关闭按钮不起作用

javascript - 服务器返回重定向 (302) 后,iOS 设备上的 AJAX 或 XHR 请求失败,代码为 0

javascript - 如何将 javascript 变量放入 url() 函数中?

javascript - 用数组元素替换字符串键

javascript - JS - 创建新 worker 导致 404