cb-ify

1.1.0 • Public • Published

cb-ify

callbackify your promise-returning fns

usage

const cbify = require('cb-ify')
 
const fn = cbify(async () => {
  return 42
})
 
fn((err, result) => {
  if (err) throw err
  console.log(result)
})

you can specify a context to bind to as the 2nd arg

const cat = {
  name: 'speckles',
  meow: async function(){ return `my name is ${this.name}` },
}
const fn = cbify(cat.meow, cat)
fn((err, response) => console.log(response))
// => "my name is speckles"

also works with sync fns

const fn = cbify(() => 42)
 
fn((err, result) => {
  if (err) throw err
  console.log(result)
})

for objects it will return a new obj with all fns cbify'd and bound to the original obj or the specified context

const obj = cbify({
  a: async () => 'a',
  b: async () => 'b',
})
 
obj.a((err, result) => {
  if (err) throw err
  console.log(result)
})

Readme

Keywords

none

Package Sidebar

Install

npm i cb-ify

Weekly Downloads

1

Version

1.1.0

License

ISC

Last publish

Collaborators

  • kumavis