(
fetchJob: TaskFetchFn,
parseResult: TaskParseFn<T>,
options: TaskPollOptions = {},
token?: AbortToken,
)
| 1089 | |
| 1090 | const pollTask = async <T>( |
| 1091 | fetchJob: TaskFetchFn, |
| 1092 | parseResult: TaskParseFn<T>, |
| 1093 | options: TaskPollOptions = {}, |
| 1094 | token?: AbortToken, |
| 1095 | ): Promise<T> => { |
| 1096 | const maxAttempts = options.maxAttempts ?? 303; |
| 1097 | const intervalMs = options.intervalMs ?? 2000; |
| 1098 | |
| 1099 | for (let i = 0; i < maxAttempts; i++) { |
| 1100 | token?.throwIfAborted(); |
| 1101 | |
| 1102 | const data = await retryWithFixedDelay( |
| 1103 | () => fetchJob(token), |
| 1104 | 2, |
| 1105 | 1000, |
| 1106 | token, |
| 1107 | ); |
| 1108 | const result = parseResult(data); |
| 1109 | |
| 1110 | if (result.status === "failed") { |
| 1111 | throw new Error(result.errorMessage || "任务执行失败"); |
| 1112 | } |
| 1113 | |
| 1114 | if (result.status === "succeeded") { |
| 1115 | if (result.result === undefined) { |
| 1116 | throw new Error("任务成功但未返回结果"); |
| 1117 | } |
| 1118 | return result.result; |
| 1119 | } |
| 1120 | |
| 1121 | await sleep(intervalMs, token); |
| 1122 | } |
| 1123 | |
| 1124 | throw new Error("任务执行超时"); |
| 1125 | }; |
| 1126 | |
| 1127 | interface MessageOptions { |
| 1128 | parseMode?: string; |
| 1129 | linkPreview?: boolean; |
nothing calls this directly
no test coverage detected