MCPcopy
hub / github.com/ocsjs/ocsjs / waitFor

Function waitFor

packages/scripts/src/utils/study.ts:56–76  ·  view source on GitHub ↗
(predicate: () => T, opts?: { timeout_seconds?: number; check_period_ms?: number })

Source from the content-addressed store, hash-verified

54}
55
56export function waitFor<T>(predicate: () => T, opts?: { timeout_seconds?: number; check_period_ms?: number }) {
57 return new Promise<T>((resolve, reject) => {
58 let timeout: any;
59 const interval = setInterval(() => {
60 const result = predicate();
61 if (result) {
62 clearInterval(interval);
63 timeout && clearTimeout(timeout);
64 resolve(result);
65 }
66 }, opts?.check_period_ms || 1000);
67
68 // 超时跳过
69 if (opts?.timeout_seconds) {
70 timeout = setTimeout(() => {
71 clearInterval(interval);
72 resolve(undefined as any);
73 }, (opts?.timeout_seconds || 10) * 1000);
74 }
75 });
76}

Callers 2

networkIdleRequireFunction · 0.90
waitForElementFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected