catering
Simple utility to allow your module to be consumed with a callback or promise
Last updated 3 years ago by vweevers .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install catering 
SYNC missed versions from official npm registry.

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 Travis build status AppVeyor build status JavaScript Style Guide

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.

Current Tags

  • 2.1.1                                ...           latest (3 years ago)

4 Versions

  • 2.1.1                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)

Copyright 2013 - present © cnpmjs.org | Home |