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: 6 months ago
JSON representation
A featherweight Promise composition and error handling library for JavaScript
- Host: GitHub
- URL: https://github.com/emphori/compose
- Owner: emphori
- License: mit
- Created: 2022-06-23T10:55:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-28T23:18:59.000Z (almost 2 years ago)
- Last Synced: 2025-01-25T17:09:20.671Z (10 months ago)
- Topics: functional, functional-programming, javascript, nodejs, promise, promise-chain, typescript
- Language: TypeScript
- Homepage:
- Size: 33.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
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