const response = await poll(
async () => this.account.client.getTx(tx.transactionHash),
(tx) => tx?.code === 0,
2000
)
Last active
August 9, 2022 15:18
-
-
Save srph/e59be3d11e608065209ebf039054d17b to your computer and use it in GitHub Desktop.
JS: Polling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { delay } from './time' | |
export async function poll<T = any>(callback: () => Promise<T>, condition: (t: T) => boolean, ms: number): Promise<T> { | |
let first = true | |
while (true) { | |
const data = first ? await callback() : await delay(ms).then(callback) | |
if (condition(data) === true) { | |
return data | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment