import type { Nullable } from '../types/misc.js'; /** * The idea here is to wrap a promise (or promise getter) so it will never throw natively, but still be able to provide success/fail results: * - `onSuccess` for handling successful result: like `then` but to be called only if there was no error * - `onError` for handling error: like `catch` but with extended error format * - `expectError` adds a specific Error sub-class caster and adds it to the error data; useful when expecting some kind of specific error * * Important: all methods are chainable however will return the same PromiseExtended instance, * while native Promise methods return a new Promise instance on each `then`/`catch`/`finally` call. * Internally it just replaces the promise reference. * * Usage example: * * ```ts * function doWork() { * return PromiseExtended * .run(async () => { * // return 42; * throw new Error('Some error'); * }); * } * * dowWork() * // if no error, will return 42 * .onSuccess(data => console.log('Success:', data)) * // if error, will return { error: 'Some error', source: Error } * .onError(({ error, source }) => console.error('Error Message:', error, '; original:', source)) * // this is possible to be called only if handlers throw * .catch(err => console.error('Really unexpected error:', err)); * * ``` * * NOTE: regular `then`/`catch` will still work, but they will go after `catch` in the chain, * so basically every `then` will be called unless some previous handler throws, * and every `catch` will NOT be called unless there was an error in any previous handler. * * If passed promise resolve is `PromiseExtended` instance, current instance will redirect everything to it. * */ export declare class PromiseExtended = Record> implements Promise { protected _error: PromiseExtended.ErrorData | null; private _promise; private readonly _errorProcessors; private constructor(); static run = Record>(worker: Promise | (() => Promise)): PromiseExtended; static succeeded(data: T): PromiseExtended; static errored>(source: Error | Error[] | string): PromiseExtended; onError(cb: Nullable<(data: PromiseExtended.ErrorData & TCustomErrors) => void>): this; onSuccess(cb: Nullable<(data: T) => void>): this; expectError(config: PromiseExtended.ErrorConfig): PromiseExtended>; expectError(name: TName, ErrCtor: new (...args: any[]) => TError2, processor?: (value: TError2) => void | Partial>): PromiseExtended>; then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | null): PromiseExtended; catch(onrejected?: ((reason: unknown) => TResult | PromiseLike) | null): PromiseExtended; finally(onfinally?: (() => void) | null): this; toSuccessPromise(): Promise; get [Symbol.toStringTag](): string; private getError; private _catch; /** Adds to the end of chain special marker that allow outer PromiseExtended to correctly process this instance with its own onSuccess/onError callbacks. * * See `example for combining` test case for more details. */ pop(): PromiseExtended; protected onErrorCaught(data: PromiseExtended.ErrorData): void; } export declare namespace PromiseExtended { type ErrorData = { error: string; source: Error; }; type ErrorConfig = { name: TName; ErrCtor: new (...args: any[]) => TError2; processor?: (value: TError2) => void | Partial>; }; namespace ErrorConfig { function createExpecter(config: Omit, 'processor'>): >(promise: PromiseExtended, processor?: (error: TError2) => void | Partial>) => PromiseExtended>; } }