Socket
Socket
Sign inDemoInstall

universalify

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

universalify

Make a callback- or promise-based function support both promises and callbacks.


Version published
Maintainers
1
Weekly downloads
70,481,847
decreased by-7.68%

Weekly downloads

Package description

What is universalify?

The universalify npm package is designed to convert callback-based functions to ones that return promises, allowing for both callback and promise-based usage. This is particularly useful for supporting legacy code while taking advantage of the benefits of promises in newer code.

What are universalify's main functionalities?

fromCallback

Converts a function that uses a callback to one that returns a promise, but still supports the callback interface.

const universalify = require('universalify');
const fs = require('fs');
const readFileAsync = universalify.fromCallback(fs.readFile);

// Using as a promise
readFileAsync('example.txt', 'utf8').then(contents => {
  console.log(contents);
}).catch(error => {
  console.error('Error reading file:', error);
});

// Using with a callback
readFileAsync('example.txt', 'utf8', (error, contents) => {
  if (error) {
    console.error('Error reading file:', error);
    return;
  }
  console.log(contents);
});

fromPromise

Converts a promise-returning function to one that supports both promises and callbacks.

const universalify = require('universalify');
const fsPromises = require('fs').promises;
const readFile = universalify.fromPromise(fsPromises.readFile);

// Using as a promise
readFile('example.txt', 'utf8').then(contents => {
  console.log(contents);
}).catch(error => {
  console.error('Error reading file:', error);
});

// Using with a callback
readFile('example.txt', 'utf8', (error, contents) => {
  if (error) {
    console.error('Error reading file:', error);
    return;
  }
  console.log(contents);
});

Other packages similar to universalify

Readme

Source

universalify

Travis branch Coveralls github branch npm npm

Make a callback- or promise-based function support both promises and callbacks.

Uses the native promise implementation.

Installation

npm install universalify

API

universalify.fromCallback(fn)

Takes a callback-based function to universalify, and returns the universalified function.

Function must take a callback as the last parameter that will be called with the signature (error, result). universalify does not support calling the callback with more than three arguments, and does not ensure that the callback is only called once.

function callbackFn (n, cb) {
  setTimeout(() => cb(null, n), 15)
}

const fn = universalify.fromCallback(callbackFn)

// Works with Promises:
fn('Hello World!')
.then(result => console.log(result)) // -> Hello World!
.catch(error => console.error(error))

// Works with Callbacks:
fn('Hi!', (error, result) => {
  if (error) return console.error(error)
  console.log(result)
  // -> Hi!
})

universalify.fromPromise(fn)

Takes a promise-based function to universalify, and returns the universalified function.

Function must return a valid JS promise. universalify does not ensure that a valid promise is returned.

function promiseFn (n) {
  return new Promise(resolve => {
    setTimeout(() => resolve(n), 15)
  })
}

const fn = universalify.fromPromise(promiseFn)

// Works with Promises:
fn('Hello World!')
.then(result => console.log(result)) // -> Hello World!
.catch(error => console.error(error))

// Works with Callbacks:
fn('Hi!', (error, result) => {
  if (error) return console.error(error)
  console.log(result)
  // -> Hi!
})

License

MIT

Keywords

FAQs

Last updated on 20 Jun 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc