2.1.4 • Published 4 years ago
@sweet-monads/async v2.1.4
@sweet-monads/async
Package for better developer experience with Promise
and @sweet-monad/interfaces/Monad
This library belongs to sweet-monads project
sweet-monads — easy-to-use monads implementation with static types definition and separated packages.
- No dependencies, one small file
- Easily auditable TypeScript/JS code
- Check out all libraries: maybe, iterator, interfaces,
Usage
npm install @sweet-monads/async
import { chain } from "@sweet-monads/async";
import { right } from "@sweet-monads/either";
const fn = () =>
Promise.resolve(right("Hello, Sweet Monad."))
.then(chain(doSomethingAsync))
.then(chain(doOneMoreAsyncThing));
API
chain
chain<T, R>(fn: (v: T) => Promise<Monad<R>>): (m: Monad<T>) => Promise<Monad<R>>;
f: (v: T) => Promise<Monad<R>>
- async function which convert a Monad to another one.- Returns
Promise
with mapped byfn
function value wrapped byMonad
by theMonad
implementation rule.
Example:
declare function getUser(email: string): Promise<Either<UserNotFoundError, User>>;
declare function arePasswordsMatched(password: string, user: User): Promise<Either<WrongCredentialsError, boolean>>;
// result is Promise<Either<UserNotFoundError | WrongCredentialsError, boolean>>
const result = getUser("some@email.com")
.then(chain(user => arePasswordsMatched("some password", user.password)));
License
MIT (c) Artem Kobzar see LICENSE file.
2.1.4
4 years ago