Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
39 lines
1002 B
TypeScript
39 lines
1002 B
TypeScript
import type { IExecuteFunctions } from 'n8n-workflow';
|
|
import { NodeApiError } from 'n8n-workflow';
|
|
|
|
export async function pollUntilAvailable<TResponse>(
|
|
ctx: IExecuteFunctions,
|
|
request: () => Promise<TResponse>,
|
|
check: (response: TResponse) => boolean,
|
|
timeoutSeconds: number,
|
|
intervalSeconds = 5,
|
|
): Promise<TResponse> {
|
|
const abortSignal = ctx.getExecutionCancelSignal();
|
|
let response: TResponse | undefined;
|
|
const startTime = Date.now();
|
|
|
|
while (!response || !check(response)) {
|
|
const elapsedTime = Date.now() - startTime;
|
|
if (elapsedTime >= timeoutSeconds * 1000) {
|
|
throw new NodeApiError(ctx.getNode(), {
|
|
message: 'Timeout reached',
|
|
code: 500,
|
|
});
|
|
}
|
|
|
|
if (abortSignal?.aborted) {
|
|
throw new NodeApiError(ctx.getNode(), {
|
|
message: 'Execution was cancelled',
|
|
code: 500,
|
|
});
|
|
}
|
|
|
|
response = await request();
|
|
|
|
// Wait before the next polling attempt
|
|
await new Promise((resolve) => setTimeout(resolve, intervalSeconds * 1000));
|
|
}
|
|
|
|
return response;
|
|
}
|