-
Notifications
You must be signed in to change notification settings - Fork 0
wait for condition
HardlyDifficult edited this page Apr 30, 2026
·
1 revision
waitForCondition repeatedly invokes an async check function until it returns something other than null or undefined, or until wall-clock time exceeds timeout. It lives in core/utils and is re-exported from the package root alongside other utilities.
Use it for orchestration scripts and integration tests when you must wait for ledger-visible state (wallet balance, contract visibility) without blocking the event loop with blind sleeps.
import { waitForCondition } from '@fairmint/canton-node-sdk';import { Canton, waitForCondition } from '@fairmint/canton-node-sdk';
const canton = new Canton({
network: 'localnet',
partyId: 'OWN_PARTY_ID',
});
const balanceStr = await waitForCondition(async () => {
const wallet = await canton.validator.getWalletBalance();
const total = wallet.effective_unlocked_qty ?? '0';
const n = parseFloat(total);
if (Number.isFinite(n) && n > 0) {
return total;
}
return undefined;
}, {
timeout: 60_000,
interval: 2_000,
timeoutMessage: 'Wallet still empty after 60s',
});
console.log('Funded:', balanceStr);waitForCondition(check, options?)
-
check(required,() => Promise<T | null | undefined>) — Returnnullorundefinedto keep polling; return any other value (including0,false,'') to succeed immediately with that value. -
options.timeout(optional, number, default30000) — Maximum milliseconds before failure. -
options.interval(optional, number, default1000) — Delay between attempts. -
options.timeoutMessage(optional, string, default'Timeout waiting for condition') — Message on the thrownOperationError.
Promise<T> — First successful non-null/non-undefined result.
On timeout throws OperationError with code OperationErrorCode.TRANSACTION_FAILED and context { timeoutMs } (see errors).
-
Tight loops: Very small
intervalagainst remote APIs can amplify rate limits — align with participant tolerance. -
Falsy success: Remember
0/false/''stop polling; returnnull/undefinedexplicitly while waiting.
-
createParty— useswaitForConditioninternally for transfer settlement. -
Errors —
OperationError.
src/core/utils/polling.ts on GitHub.