| 448 | * @return a promise wrapping the original promise with rejection on timeout |
| 449 | */ |
| 450 | export function withTimeout<T>(timeoutMs: number, promise: Promise<T>): Promise<T> { |
| 451 | return new Promise<T>((resolve, reject) => { |
| 452 | const timeout = setTimeout(() => reject(new Error("Timed out.")), timeoutMs); |
| 453 | promise.then( |
| 454 | (value) => { |
| 455 | clearTimeout(timeout); |
| 456 | resolve(value); |
| 457 | }, |
| 458 | (err) => { |
| 459 | clearTimeout(timeout); |
| 460 | reject(err); |
| 461 | }, |
| 462 | ); |
| 463 | }); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Resolves all Promises at every key in the given object. If a value is not a |