Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vweevers/catering
Simple utility to allow your module to be consumed with a callback or promise.
https://github.com/vweevers/catering
callback nodejs npm-package promise
Last synced: 6 days ago
JSON representation
Simple utility to allow your module to be consumed with a callback or promise.
- Host: GitHub
- URL: https://github.com/vweevers/catering
- Owner: vweevers
- License: mit
- Created: 2018-09-01T08:44:33.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-01T07:42:44.000Z (about 1 year ago)
- Last Synced: 2024-11-02T01:42:14.805Z (13 days ago)
- Topics: callback, nodejs, npm-package, promise
- Language: JavaScript
- Homepage:
- Size: 20.5 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# catering
**Cater to callback and promise crowds.**
Simple utility to allow your module to be consumed with a callback or promise. For Node.js and browsers.[![npm status](https://img.shields.io/npm/v/catering.svg)](https://www.npmjs.com/package/catering)
[![Node version](https://img.shields.io/node/v/catering.svg)](https://www.npmjs.com/package/catering)
[![Test](https://img.shields.io/github/workflow/status/vweevers/catering/Test?label=test)](https://github.com/vweevers/catering/actions/workflows/test.yml)
[![Standard](https://img.shields.io/badge/standard-informational?logo=javascript&logoColor=fff)](https://standardjs.com)## Menu
If your module internally uses callbacks:
```js
const { fromCallback } = require('catering')
const kPromise = Symbol('promise')module.exports = function (callback) {
callback = fromCallback(callback, kPromise)
queueMicrotask(() => callback(null, 'example'))
return callback[kPromise]
}
```If your module internally uses promises:
```js
const { fromPromise } = require('catering')module.exports = function (callback) {
return fromPromise(Promise.resolve('example'), callback)
}
```Either way your module can now be consumed in two ways:
```js
example((err, result) => {})
const result = await example()
```When converting from a promise to a callback, `fromPromise` calls the callback in a next tick to escape the promise chain and not let it steal your beautiful errors.
## Install
With [npm](https://npmjs.org) do:
```
npm install catering
```## License
[MIT](LICENSE) © 2018-present Vincent Weevers. Originally extracted from [`levelup`](https://github.com/Level/levelup/blob/37e0270c8c29d5086904e29e247e918dddcce6e2/lib/promisify.js).