export default class CachePromise { expireTime: number; curTm: number = 0; promise: Promise | undefined; pf: () => Promise; constructor(pf: () => Promise, expireTime?: number) { this.pf = pf; this.expireTime = expireTime || 0; } setNull = () => { this.promise = undefined; } get = async () => { const now = Date.now(); if (this.expireTime && now - this.curTm > this.expireTime) { this.promise = this.pf(); this.curTm = now; } if (!this.promise) { this.promise = this.pf(); this.curTm = now; } const data = await this.promise; if (!data) { this.setNull(); } return data; } }