/** * Provides cancellation guards and timeouts for asynchronous SDK runtime work. * * Use {@link Cancelable} to cancel several registered operations together. Use * {@link withTimeout} when only a deadline is needed. These helpers reject * their returned promises, but they cannot stop arbitrary source promises. * * {@link CancelableSleep} and {@link sleep} are currently unusable on supported * Node.js runtimes because of a Promise-subclass compatibility problem. Do not * use them. * * @packageDocumentation */ /** Reports that a {@link Cancelable} operation was canceled. */ export declare class CancelError extends Error { /** Creates a new cancel error. */ constructor(); } /** Reports that {@link withTimeout} reached its deadline. */ export declare class TimeoutError extends Error { /** Creates a new timeout error. */ constructor(); } /** * Coordinates cancellation for several waits and promises. * * One instance is a one-way lifecycle. After {@link cancel} runs, * {@link Cancelable.isCanceled} stays `true`; create a new instance for later * work. Cancellation rejects registered operations with {@link CancelError}. * It does not stop the underlying work of a promise passed to {@link guard}. * * @example * ```ts * import { * Cancelable, * CancelError, * } from '@nebius/js-sdk/runtime/util/cancelable'; * * const cancellation = new Cancelable(); * const work = cancellation.guard(new Promise(() => {})); * cancellation.cancel(); * * try { * await work; * } catch (error) { * if (error instanceof CancelError) { * // The caller requested cancellation. * } * } * ``` */ export declare class Cancelable { private _isCanceled; private timers; private cancelWaiters; /** * Cancels registered operations and clears their delay timers. * * Calling this method more than once is safe. */ cancel(): void; /** Returns whether {@link cancel} has been called. */ get isCanceled(): boolean; /** * Waits for a duration or rejects with {@link CancelError} after cancellation. * * The timer does not keep a Node.js process alive. */ sleep(ms: number): Promise; /** * Rejects the returned promise when this instance is canceled. * * The source promise continues to run because JavaScript promises do not * have a general cancellation operation. */ guard(promise: PromiseLike): Promise; /** * Applies both this instance's cancellation and a timeout to a promise. * * Neither condition stops the source promise. */ withTimeout(promise: PromiseLike, ms: number): Promise; } /** * Rejects with {@link TimeoutError} if a promise does not settle in time. * * The source promise continues to run after a timeout. Its later result is * ignored. * * @example * ```ts * import { withTimeout } from '@nebius/js-sdk/runtime/util/cancelable'; * * const source = new Promise((resolve) => { * setTimeout(() => resolve('ready'), 10); * }); * const result = await withTimeout(source, 5_000); * ``` */ export declare function withTimeout(promise: PromiseLike, ms: number): Promise; /** * Represents a delay that the caller can cancel. * * Awaiting a canceled delay rejects with {@link CancelError}. Canceling a delay * after it has completed has no effect. * * @remarks * Direct construction currently fails on supported Node.js runtimes because * this Promise subclass accesses instance state while the base Promise * constructor runs. Do not construct this class. Use a normal Promise timer * with {@link Cancelable.guard} until the implementation is corrected. */ export declare class CancelableSleep extends Promise { private handle; private rejector; /** Creates a delay in milliseconds. Its timer does not keep Node.js alive. */ constructor(ms: number); /** Cancels a pending delay. */ cancel(): void; } /** * Returns a cancellable delay. * * `setCancel` receives the cancellation function immediately. This is useful * when an API must expose cancellation separately from its returned promise. * * @remarks * This function constructs {@link CancelableSleep} and therefore fails on * supported Node.js runtimes. Do not call it until the Promise-subclass * implementation is corrected. */ export declare function sleep(ms: number, setCancel?: (cancel: () => void) => void): CancelableSleep; //# sourceMappingURL=cancelable.d.ts.map