我有一个可以在构造时接受回调的类,以便它可以将数据发送回其父级。
class Chain {
_callback: (data: ChainData) => void;
constructor(callback: (data: ChainData) => void) {
this._callback = callback;
setInterval(this._refresh, 13 * 1000);
}
private async _refresh() {
this._callback({
height: 1,
difficulty: 2,
hashrate: 3,
blocktime: 4
});
}
}
我发现有趣的是,它可以正常工作,但是每次调用时都会收到此错误。同样,它在功能上100%正常工作。
(node:26146) UnhandledPromiseRejectionWarning: TypeError: this._callback is not a function
at Timeout.<anonymous> (/Users/lukepighetti/code/typescript/bot/lib/services/chain.ts:17:10)
at Generator.next (<anonymous>)
at fulfilled (/Users/lukepighetti/code/typescript/bot/lib/services/chain.ts:4:58)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
我在做错什么,如何摆脱这个看似毫无意义的错误?