catering
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

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 Node version Test Standard

Menu

If your module internally uses callbacks:

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:

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:

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 do:

npm install catering

License

MIT © 2018-present Vincent Weevers. Originally extracted from levelup.

Package Sidebar

Install

npm i catering

Weekly Downloads

182,164

Version

2.1.1

License

MIT

Unpacked Size

5 kB

Total Files

7

Last publish

Collaborators

  • vweevers