MCPcopy Create free account
hub / github.com/modelcontextprotocol/typescript-sdk / waitForTaskStatus

Function waitForTaskStatus

test/helpers/tasks.ts:6–33  ·  view source on GitHub ↗
(
    getTask: (taskId: string) => Promise<Task | null | undefined>,
    taskId: string,
    desiredStatus: Task['status'],
    {
        intervalMs = 100,
        timeoutMs = 10_000
    }: {
        intervalMs?: number;
        timeoutMs?: number;
    } = {}
)

Source from the content-addressed store, hash-verified

4 * Polls the provided getTask function until the task reaches the desired status or times out.
5 */
6export 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}

Callers 1

Calls 1

getTaskFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…