predictype
Version:
PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.
32 lines (31 loc) • 1.19 kB
TypeScript
import { PromiseStateOper } from '../../enums/promises.js';
export type PromiseWithState<T> = {
promise: Promise<T>;
state: PromiseStateOper;
};
export declare function wrapPromise<T>(p: Promise<T>): PromiseWithState<T>;
/**
* Checks the state of a wrapped Promise using the specified operation.
*
* Note: This predicate requires a wrapper or custom Promise implementation that exposes state.
* Standard JS Promises do not expose their state synchronously.
*
* @param wrapper The wrapped Promise with state.
* @param oper The state operation to perform (e.g. 'is_pending', 'is_fulfilled').
* @returns True if the state check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const p = Promise.resolve(42);
* const wrapped = wrapPromise(p);
*
* promiseState(wrapped, 'is_pending'); // true (immediately after wrapping)
*
* @remarks
* Supported Operators:
* - **IS_PENDING**: Promise is pending
* - **IS_FULFILLED**: Promise is fulfilled
* - **IS_REJECTED**: Promise is rejected
*/
export declare function promiseState<T>(wrapper: PromiseWithState<T>, oper: PromiseStateOper): boolean;