typescript-monads
Version:
Write cleaner TypeScript
34 lines • 1.34 kB
JavaScript
import { maybe } from '../maybe.factory';
/**
* Converts a Promise to a Maybe monad
*
* Creates a Maybe from a Promise that resolves to a value.
* If the promise resolves successfully, returns Some with the resolved value.
* If the promise rejects or resolves to null/undefined, returns None.
*
* Note on resolution preservation: This transformation preserves resolution
* semantics by converting:
* - Promise rejections to None values (representing failure/absence)
* - Promise resolutions to Some values (representing success/presence)
*
* The asynchronous nature is maintained by returning a Promise that resolves to a Maybe,
* rather than a Maybe directly. This allows for proper handling in asynchronous chains.
*
* @param promise The promise to convert to a Maybe
* @returns A Promise that resolves to a Maybe containing the result
*
* @example
* // Convert a promise to a Maybe and use in a chain
* api.fetchUser(userId)
* .then(promiseToMaybe)
* .then(userMaybe => userMaybe.match({
* some: user => console.log(user.name),
* none: () => console.log('User not found')
* }))
*/
export function promiseToMaybe(promise) {
return promise
.then(function (value) { return maybe(value); })
.catch(function () { return maybe(); });
}
//# sourceMappingURL=promise-to-maybe.js.map