How to implement a Deferred promise that extends Promise? It's important to extend Promise for type-safe usage where a typical Promise is expected.
Following implementation
export class Deferred<T> extends Promise<T> {
public _resolveSelf;
public _rejectSelf;
constructor() {
super(
(resolve, reject) =>
{
this._resolveSelf = resolve
this._rejectSelf = reject
}
)
}
public resolve(val:T) { this._resolveSelf(val) }
public reject(reason:any) { this._rejectSelf(reason) }
}
throws TypeError: _this is undefined
.
In this Typescript playground, one can see that the compiled javascript is funny. In line 15, during declaration of _this
already its properties are being assigned.