/** * @module * The definition of {@linkcode Rhodium}. */ import type { Errored, Merged, ToRhodium } from "./terminology.mts"; import type { isExcludeUnsafe } from "./internal/subtypeDetection.mts"; import * as CancelErrors from "./err/CancelErrors.mts"; import * as TimeoutErrors from "./err/TimeoutErrors.mts"; import { oneSettled } from "./settled.mts"; import { oneFinalized, type RhodiumFinalizedResult } from "./finalized.mts"; import { type Cancelled } from "./internal/cancellableCallback.mts"; /** * A {@linkcode Promise} wrapper that adds syntax sugar. * `R` is the type of an {@link Awaited awaited} {@linkcode Rhodium}. * `E` is the {@link Errored error} which may be thrown by a {@linkcode Rhodium}. */ export declare class Rhodium { #private; /** * Creates a new {@linkcode Rhodium}. * @param promise object to wrap. */ constructor(promise: PromiseLike); /** * Creates a new {@linkcode Rhodium}. * @param executor A callback used to initialize the Rhodium. * This callback is passed two arguments: a resolve callback used to resolve the Rhodium with a value or the result of another {@link PromiseLike promise-like}, * and a reject callback used to reject the Rhodium with a provided reason or error. */ constructor(executor: (resolve: (value: R | Rhodium) => void, reject: (reason: E) => void, signal: AbortSignal) => void); /** * Attaches callbacks for the resolution and/or rejection of the Rhodium. * * **Throws {@linkcode CancelErrors.CannotAttachConsumerError} synchronously, when called on a cancelled Rhodium**. * @param onfulfilled The callback to execute when the Rhodium is resolved. * @param onrejected The callback to execute when the Rhodium is rejected. * @returns A Rhodium for the completion of which ever callback is executed. */ then, P2 = Rhodium>(onfulfilled?: ((value: NoInfer, signal: AbortSignal) => P1) | null | undefined, onrejected?: ((reason: NoInfer, signal: AbortSignal) => P2) | null | undefined): Merged | ToRhodium>; /** * Attaches a callback for only the rejection of the Rhodium. * Syntax sugar for {@linkcode then}. * @param onrejected The callback to execute when the Rhodium is rejected. * @returns A Rhodium for the completion of the callback. */ catch>(onrejected?: ((reason: NoInfer) => P1) | null | undefined): Merged | Rhodium>; /** * Attaches a callback for some specific rejections of the Rhodium. * Syntax sugar for {@linkcode catch}. * @param filter The callback that decides which reasons will be handled. * @param onrejected The callback to execute when the Rhodium is rejected with an allowed reason. */ catchFilter>, const P1 = Rhodium>(filter: (reason: E) => reason is EF, onrejected: (reason: EF) => P1): Merged | Rhodium | Rhodium>; /** * Attaches a callback that is invoked when the Rhodium is settled (fulfilled or rejected). * The resolved value cannot be modified from the callback, but the rejected value can be. * @param onfinally The callback to execute when the Rhodium is settled. * @returns A Rhodium for the completion of the callback. */ finally>(onfinally?: (() => P1) | null | undefined): Merged>>; /** * Merges both the resolution and rejection into resolution. * Syntax sugar for calling {@linkcode oneSettled} on this Rhodium. * * @returns a Rhodium that is resolved when this Rhodium resolves or rejects. */ settled(): ReturnType>; /** * Attaches a time constraint to *this* Rhodium. * The resolution and rejection values are passed through. * * If it fails to {@link settled settle} in the given time, {@linkcode TimeoutErrors.TimeoutError TimeoutError} is rejected, * and this Rhodium gets {@link cancel cancelled} as an optimization, * because the fact of its settlement would not matter anymore. * * @param ms Amount of milliseconds to wait before rejecting. */ timeout(ms: number): Merged>; /** * Nested {@linkcode Promise} object this {@linkcode Rhodium} was created with. */ get promise(): Promise; /** * Sets the {@linkcode child}'s parent field, and automatically un-sets it * when the {@linkcode parent} has been fulfilled. * * Keeps track of children amount on the parent as well. */ private static attachChildToParent; /** * This signal triggers for the **currently executing Rhodiums** in a chain, when that chain gets cancelled. * If this Rhodium has resolved *or* is waiting for preceding Rhodiums, signal will not be triggered, * because the operation that could be aborted has not started yet. */ get signal(): AbortSignal; /** Returns whether this Rhodium was cancelled or not. */ get cancelled(): boolean; /** * Creates a Rhodium that is resolved when the provided value finalizes. * Finalization is a superset of both {@link cancel cancellation} and {@link settled settlement}. * In case of cancellation, returned Rhodium will only resolve when all {@linkcode finally} callbacks have been executed. */ finalized(): ReturnType>; /** * **Synchronous, even though it does return Rhodium.** * * Must be called on the last {@linkcode Rhodium} of its chain. * Rejects {@linkcode CancelErrors.CannotBeCancelledError} otherwise. * * Cancels this Rhodium, and the preceding Rhodiums, up to the root *or* the point of divergence from another pending chain. * Cancelled Rhodiums do not execute their consumers, except for {@linkcode finally}. * @returns Calls {@linkcode finalized} and returns the result. */ cancel(): Rhodium, CancelErrors.CannotBeCancelledError>; /** * When `yield`ed, Rhodium expects the resume value provided in `next` to be *itself*, awaited. * This enables {@linkcode tryGen}. */ [Symbol.iterator](): { next(resolved: [never] extends [R] ? any : R): IteratorResult, R>; }; }