我试图延伸这两个承诺:
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',那么它对我有什么要求?
答案 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)));
});