typescript-monads
Version:
Write cleaner TypeScript
42 lines • 1.44 kB
JavaScript
import { ok, fail } from '../result.factory';
/**
* Converts a Promise to a Result monad
*
* Creates a Result from a Promise:
* - If the promise resolves, returns an Ok Result with the resolved value
* - If the promise rejects, returns a Fail Result with the rejection reason
*
* Note on error typing: The error type defaults to unknown because JavaScript promises
* can reject with any value. In TypeScript, you may need to use type assertions or
* create a more specific version of this function if you know the exact error type.
*
* @param promise The promise to convert to a Result
* @returns A Promise that resolves to a Result containing either the resolved value or rejection reason
*
* @example
* // Convert a promise to a Result
* fetchData()
* .then(promiseToResult)
* .then(result => result.match({
* ok: data => renderData(data),
* fail: error => showError(error)
* }));
*
* // With Promise chaining
* promiseToResult(fetchData())
* .then(result => {
* if (result.isOk()) {
* const data = result.unwrap();
* return renderData(data);
* } else {
* const error = result.unwrapFail();
* return showError(error);
* }
* });
*/
export function promiseToResult(promise) {
return promise
.then(function (value) { return ok(value); })
.catch(function (error) { return fail(error); });
}
//# sourceMappingURL=promise-to-result.js.map