Socket
Book a DemoInstallSign in
Socket

promisize

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promisize

Easily support both callbacks and promises in your js lib

1.1.2
latest
Source
npmnpm
Version published
Weekly downloads
1.4K
-15.52%
Maintainers
1
Weekly downloads
 
Created
Source

promisize Build Status npm

Easily support both callbacks and promises in your js lib

promisize takes your function's callback argument and returns a new callback with a promise property. When the new callback is called, it either calls the original callback or if no callback was given it fulfills the promise. promisize is lightweight and does not include any promise shims or dependencies but instead uses the globally defined Promise. If Promise is not defined (i.e. no native promise support) then callbacks still work as they would without promisize.

Usage

var promisize = require('promisize')

function someAsyncFunction (cb) {
  cb = promisize(cb)

  // Do some async stuff then
  setTimeout(function () {
    cb(null, 'foobar')
  })

  // Undefined if there is no Promise support
  // or if the original callback was defined
  return cb.promise
}

API

var cb = promisize(function callback (err, result) {})

promisize takes a nodejs style callback and returns another nodejs style callback. When the returned callback is called it either calls the original callback or fulfills the promise but never both.

cb.promise

The returned cb has a promise property that is only defined if the environment has Promise support (i.e. Promise is defined) and the original callback was undefined. When the returned callback is called it fulfills this promise if it is defined or calls the original callback if the promise is undefined.

License

MIT. Copyright (c) Austin Middleton.

Keywords

defer

FAQs

Package last updated on 14 Apr 2017

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.