| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | /** |
| b69ab31 | | | 9 | * Takes an existing Promise and wraps it with a new Promise that will either: |
| b69ab31 | | | 10 | * - be fulfilled with the result from the original promise |
| b69ab31 | | | 11 | * - be rejected with the provided error message after `timeoutInMillis` |
| b69ab31 | | | 12 | * milliseconds. |
| b69ab31 | | | 13 | * |
| b69ab31 | | | 14 | * Note that in the case where the returned Promise rejects, there is nothing |
| b69ab31 | | | 15 | * that stops the execution of the executor function used to create the |
| b69ab31 | | | 16 | * original Promise. |
| b69ab31 | | | 17 | */ |
| b69ab31 | | | 18 | export default function rejectAfterTimeout<T>( |
| b69ab31 | | | 19 | promise: Promise<T>, |
| b69ab31 | | | 20 | timeoutInMillis: number, |
| b69ab31 | | | 21 | message: string, |
| b69ab31 | | | 22 | ): Promise<T> { |
| b69ab31 | | | 23 | return Promise.race([ |
| b69ab31 | | | 24 | promise, |
| b69ab31 | | | 25 | new Promise<T>((_resolve, reject) => { |
| b69ab31 | | | 26 | setTimeout(() => reject(message), timeoutInMillis); |
| b69ab31 | | | 27 | }), |
| b69ab31 | | | 28 | ]); |
| b69ab31 | | | 29 | } |