1. catering
Simple utility to allow your module to be consumed with a callback or promise
catering
Package: catering
Created by: vweevers
Last modified: Tue, 12 Apr 2022 05:27:45 GMT
Version: 2.1.1
License: MIT
Downloads: 733,523
Repository: https://github.com/vweevers/catering

Install

npm install catering
yarn add catering

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

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.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code