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: about 1 year 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 (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-01T07:42:44.000Z (over 2 years ago)
- Last Synced: 2025-04-30T06:44:28.715Z (about 1 year 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.
[](https://www.npmjs.com/package/catering)
[](https://www.npmjs.com/package/catering)
[](https://github.com/vweevers/catering/actions/workflows/test.yml)
[](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).