1 | ;
|
2 | /*
|
3 | * Copyright The OpenTelemetry Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * https://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.callWithTimeout = exports.TimeoutError = void 0;
|
19 | /**
|
20 | * Error that is thrown on timeouts.
|
21 | */
|
22 | class TimeoutError extends Error {
|
23 | constructor(message) {
|
24 | super(message);
|
25 | // manually adjust prototype to retain `instanceof` functionality when targeting ES5, see:
|
26 | // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
|
27 | Object.setPrototypeOf(this, TimeoutError.prototype);
|
28 | }
|
29 | }
|
30 | exports.TimeoutError = TimeoutError;
|
31 | /**
|
32 | * Adds a timeout to a promise and rejects if the specified timeout has elapsed. Also rejects if the specified promise
|
33 | * rejects, and resolves if the specified promise resolves.
|
34 | *
|
35 | * <p> NOTE: this operation will continue even after it throws a {@link TimeoutError}.
|
36 | *
|
37 | * @param promise promise to use with timeout.
|
38 | * @param timeout the timeout in milliseconds until the returned promise is rejected.
|
39 | */
|
40 | function callWithTimeout(promise, timeout) {
|
41 | let timeoutHandle;
|
42 | const timeoutPromise = new Promise(function timeoutFunction(_resolve, reject) {
|
43 | timeoutHandle = setTimeout(function timeoutHandler() {
|
44 | reject(new TimeoutError('Operation timed out.'));
|
45 | }, timeout);
|
46 | });
|
47 | return Promise.race([promise, timeoutPromise]).then(result => {
|
48 | clearTimeout(timeoutHandle);
|
49 | return result;
|
50 | }, reason => {
|
51 | clearTimeout(timeoutHandle);
|
52 | throw reason;
|
53 | });
|
54 | }
|
55 | exports.callWithTimeout = callWithTimeout;
|
56 | //# sourceMappingURL=timeout.js.map |
\ | No newline at end of file |