Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

deferinfer

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deferinfer

Alternative converter between promises and callbacks

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

deferinfer

Alternative converter between promises and callbacks

installation

npm install deferinfer

# -- or --

yarn add deferinfer
const { defer, infer } = require('promise-util')

// -- or --

const defer = require('promise-util').defer

defer(callback) -> Promise

Convert callback function into to promise

const result = await defer(done => someCallbackFunction(done))

Properly forwards error

defer(done => {
  done(new Error()) // forwarded to .catch()
  throw new Error() // forwarded to .catch()
  done(null, 42)    // forwarded to .then()
})
  .then(console.log)
  .catch(console.error)

Async method support

defer(async done => {
  const result = await anotherPromise()
  done(null, result)promise-util
})

infer(promise, callback) -> Promise

Convert promise to callback

const p = infer(Promise.resolve(42), (err, res) => {
  if (err) console.error(err)
  else console.log(res)
})

Define promise/callback agnostic functions

function add(a, b, callback) {
  const promise = defer(done => {
    done(null, a + b)
  })
  return infer(promise, callback)
}

add(5, 7, (err, sum) => {
  console.log(sum) // => 12
})

const sum = await add(6, 4)
console.log(sum) // => 10

Keywords

FAQs

Package last updated on 28 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc