Skip to content

Instantly share code, notes, and snippets.

@srph
Last active August 9, 2022 15:18
Show Gist options
  • Save srph/e59be3d11e608065209ebf039054d17b to your computer and use it in GitHub Desktop.
Save srph/e59be3d11e608065209ebf039054d17b to your computer and use it in GitHub Desktop.
JS: Polling

Usage

const response = await poll(
  async () => this.account.client.getTx(tx.transactionHash),
  (tx) => tx?.code === 0,
  2000
)
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