type AsyncFunc = (...args: any[]) => Promise export class CircularError extends Error { name = this.constructor.name constructor() { super("infinite loop detected") } } export function forbidCircularity(func: F) { let circle = false return async function(...args) { if (!circle) { circle = true const result = await func(...args) circle = false return result } else { throw new CircularError() } } }