Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/emphori/compose

A featherweight Promise composition and error handling library for JavaScript
https://github.com/emphori/compose

functional functional-programming javascript nodejs promise promise-chain typescript

Last synced: 9 days ago
JSON representation

A featherweight Promise composition and error handling library for JavaScript

Awesome Lists containing this project

README

        

# Compose

A drop-in extension for Promises, adding oodles of functional goodness through
composition and taking error handling to an entirely different level.

## Features

* **Lightweight and with almost no memory footprint,** the main package export
comes in at well below 1KB, unminified, ungzipped.

* **A familiar Promise like interface** reduces the learning curve
dramatically.

* **Robust types for TypeScript** are included in the package by default, with
specific attention in areas such as scope narrowing, for heavily nested and
complex compositions.

* **Interoperable with existing code by design,** to ensure that it's easy to
introduce incrementally to your project without any pesky migrations.

## Installation

Compose is available from the NPM registry and as a GitHub Package. Whichever
source you prefer to use, the installation should remain the same.

```sh
# Using NPM
npm install @emphori/compose -S

# Or, using Yarn
yarn add @emphori/compose
```

## Examples

```ts
import { compose, reject } from '@emphori/compose'
import type { Promise } from '@emphori/promise'

// (userId: string) => Promise
const getUserOrg = compose(getUser).then(getOrgForUser)

function getUser(userId: string): Promise {
return User.getById(userId).then((user) => {
return user ?? reject(UserNotFound)
})
}

function getOrgForUser(user: User): Promise {
return Org.getById(user.orgId).then((org) => {
return org ?? reject(OrgNotFound)
})
}
```

## Contributing

If you're interested in contributing, or just want to learn more about Compose,
then head over to the [repository][repo] where you'll hopefully find all the
information you need.

[repo]: https://github.com/emphori/compose

## Licence

This project is released under the [MIT License][license]. Enjoy responsibly ❤️

[license]: https://github.com/emphori/compose/blob/HEAD/LICENSE