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

universalify

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

universalify

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

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
91M
increased by3.23%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 01 Nov 2023

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