MCPcopy
hub / github.com/epicweb-dev/epic-stack / waitFor

Function waitFor

tests/playwright-utils.ts:143–162  ·  view source on GitHub ↗
(
	cb: () => ReturnValue | Promise<ReturnValue>,
	{
		errorMessage,
		timeout = 5000,
	}: { errorMessage?: string; timeout?: number } = {},
)

Source from the content-addressed store, hash-verified

141 * throw the error message provided as a fallback
142 */
143export async function waitFor<ReturnValue>(
144 cb: () => ReturnValue | Promise<ReturnValue>,
145 {
146 errorMessage,
147 timeout = 5000,
148 }: { errorMessage?: string; timeout?: number } = {},
149) {
150 const endTime = Date.now() + timeout
151 let lastError: unknown = new Error(errorMessage)
152 while (Date.now() < endTime) {
153 try {
154 const response = await cb()
155 if (response) return response
156 } catch (e: unknown) {
157 lastError = e
158 }
159 await new Promise((r) => setTimeout(r, 100))
160 }
161 throw lastError
162}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected