ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 1.23 kB
JavaScript
import '../functional/optional.mjs';
import { Result } from '../functional/result.mjs';
/**
* 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.
*
* @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
*
* @example
* ```typescript
* const result = await createPromise<string, Error>((resolve, reject) => {
* setTimeout(() => {
* if (Math.random() > 0.5) {
* resolve("Success!");
* } else {
* reject(new Error("Failed"));
* }
* }, 1000);
* });
*
* if (result.isOk()) {
* console.log(result.value); // "Success!"
* } else {
* console.log(result.error); // Error: Failed
* }
* ```
*/
const createPromise = (executor) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
Result.fromPromise(new Promise(executor));
export { createPromise };
//# sourceMappingURL=promise.mjs.map