1.1.0 • Published 6 years ago

cb-ify v1.1.0

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

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)
})