UNPKG

@opentelemetry/core

Version:

OpenTelemetry Core provides constants and utilities shared by all OpenTelemetry SDK packages.

1 lines • 2.16 kB
{"version":3,"file":"timeout.cjs","names":[],"sources":["../../src/utils/timeout.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/**\n * Error that is thrown on timeouts.\n */\nexport class TimeoutError extends Error {\n constructor(message?: string) {\n super(message);\n\n // manually adjust prototype to retain `instanceof` functionality when targeting ES5, see:\n // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work\n Object.setPrototypeOf(this, TimeoutError.prototype);\n }\n}\n\n/**\n * Adds a timeout to a promise and rejects if the specified timeout has elapsed. Also rejects if the specified promise\n * rejects, and resolves if the specified promise resolves.\n *\n * <p> NOTE: this operation will continue even after it throws a {@link TimeoutError}.\n *\n * @param promise promise to use with timeout.\n * @param timeout the timeout in milliseconds until the returned promise is rejected.\n */\nexport function callWithTimeout<T>(\n promise: Promise<T>,\n timeout: number\n): Promise<T> {\n let timeoutHandle: ReturnType<typeof setTimeout>;\n\n const timeoutPromise = new Promise<never>(function timeoutFunction(\n _resolve,\n reject\n ) {\n timeoutHandle = setTimeout(function timeoutHandler() {\n reject(new TimeoutError('Operation timed out.'));\n }, timeout);\n });\n\n return Promise.race([promise, timeoutPromise]).then(\n result => {\n clearTimeout(timeoutHandle);\n return result;\n },\n reason => {\n clearTimeout(timeoutHandle);\n throw reason;\n }\n );\n}\n"],"mappings":";;;;AAQA,IAAa,eAAb,MAAa,qBAAqB,MAAM;CACtC,YAAY,SAAkB;EAC5B,MAAM,OAAO;EAIb,OAAO,eAAe,MAAM,aAAa,SAAS;CACpD;AACF;;;;;;;;;;AAWA,SAAgB,gBACd,SACA,SACY;CACZ,IAAI;CAEJ,MAAM,iBAAiB,IAAI,QAAe,SAAS,gBACjD,UACA,QACA;EACA,gBAAgB,WAAW,SAAS,iBAAiB;GACnD,OAAO,IAAI,aAAa,sBAAsB,CAAC;EACjD,GAAG,OAAO;CACZ,CAAC;CAED,OAAO,QAAQ,KAAK,CAAC,SAAS,cAAc,CAAC,CAAC,CAAC,MAC7C,WAAU;EACR,aAAa,aAAa;EAC1B,OAAO;CACT,IACA,WAAU;EACR,aAAa,aAAa;EAC1B,MAAM;CACR,CACF;AACF"}