(
getTask: (taskId: string) => Promise<Task | null | undefined>,
taskId: string,
desiredStatus: Task['status'],
{
intervalMs = 100,
timeoutMs = 10_000
}: {
intervalMs?: number;
timeoutMs?: number;
} = {}
)
| 4 | * Polls the provided getTask function until the task reaches the desired status or times out. |
| 5 | */ |
| 6 | export async function waitForTaskStatus( |
| 7 | getTask: (taskId: string) => Promise<Task | null | undefined>, |
| 8 | taskId: string, |
| 9 | desiredStatus: Task['status'], |
| 10 | { |
| 11 | intervalMs = 100, |
| 12 | timeoutMs = 10_000 |
| 13 | }: { |
| 14 | intervalMs?: number; |
| 15 | timeoutMs?: number; |
| 16 | } = {} |
| 17 | ): Promise<Task> { |
| 18 | const start = Date.now(); |
| 19 | |
| 20 | // eslint-disable-next-line no-constant-condition |
| 21 | while (true) { |
| 22 | const task = await getTask(taskId); |
| 23 | if (task && task.status === desiredStatus) { |
| 24 | return task; |
| 25 | } |
| 26 | |
| 27 | if (Date.now() - start > timeoutMs) { |
| 28 | throw new Error(`Timed out waiting for task ${taskId} to reach status ${desiredStatus}`); |
| 29 | } |
| 30 | |
| 31 | await new Promise(resolve => setTimeout(resolve, intervalMs)); |
| 32 | } |
| 33 | } |
no test coverage detected
searching dependent graphs…