(promise: Thenable<T>, message: string | (() => string), seconds = 360)
| 270 | } |
| 271 | |
| 272 | export async function withTimeout<T>(promise: Thenable<T>, message: string | (() => string), seconds = 360): Promise<T> { |
| 273 | return new Promise<T>((resolve, reject) => { |
| 274 | // Set a timeout to reject the promise after the timeout period. |
| 275 | const timeoutTimer = setTimeout(() => { |
| 276 | const msg = typeof message === "string" ? message : message(); |
| 277 | reject(new Error(`${msg} within ${seconds}s`)); |
| 278 | }, seconds * 1000); |
| 279 | |
| 280 | // When the main promise completes (or rejects), cancel the timeout and return its result. |
| 281 | promise.then( |
| 282 | (result) => { |
| 283 | clearTimeout(timeoutTimer); |
| 284 | resolve(result); |
| 285 | }, |
| 286 | (e) => { |
| 287 | clearTimeout(timeoutTimer); |
| 288 | reject(e); |
| 289 | }, |
| 290 | ); |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Returns a "fixed" toolEnv to work around a Copilot env var mutation issue. |
no outgoing calls
no test coverage detected