UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

56 lines (53 loc) 1.64 kB
import { fromPromise } from '../functional/result/impl/result-from-promise.mjs'; import '@sindresorhus/is'; /** * Creates a Promise that wraps the result in a Result type for type-safe error * handling. This function is an alternative to `new Promise(executor)` that * provides enhanced type safety by returning a Result type instead of throwing * exceptions. * * @example * * ```ts * // Create a promise that resolves successfully * const successPromise = createPromise<number, string>((resolve) => { * setTimeout(() => { * resolve(42); * }, 0); * }); * * const successResult = await successPromise; * * assert.isTrue(Result.isOk(successResult)); * * if (Result.isOk(successResult)) { * assert.isTrue(successResult.value === 42); * } * * // Create a promise that rejects with an error * const errorPromise = createPromise<number, string>((_, reject) => { * setTimeout(() => { * reject('Something went wrong'); * }, 0); * }); * * const errorResult = await errorPromise; * * assert.isTrue(Result.isErr(errorResult)); * * if (Result.isErr(errorResult)) { * assert.isTrue(errorResult.value === 'Something went wrong'); * } * ``` * * @template S - The type of successful value * @template E - The type of error value * @param executor - Function that takes resolve and reject callbacks * @returns A Promise that resolves to a Result containing either success or * error */ const createPromise = (executor) => // eslint-disable-next-line total-functions/no-unsafe-type-assertion fromPromise(new Promise(executor)); export { createPromise }; //# sourceMappingURL=promise.mjs.map