Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
universalify
Advanced tools
Make a callback- or promise-based function support both promises and callbacks.
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.
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);
});
Pify is a package that promisifies functions. It converts functions that use node-style callbacks into functions that return promises. Unlike universalify, pify does not maintain callback support once promisified.
Bluebird is a comprehensive promise library that includes a promisify feature to convert callback-based functions into promise-returning functions. It is more feature-rich than universalify but also larger in size and does not support callbacks after promisification.
Util.promisify is a built-in Node.js utility function that converts a callback-based function into a promise-based one. It is similar to universalify but is limited to Node.js and does not support the original callback interface after conversion.
Make a callback- or promise-based function support both promises and callbacks.
Uses the native promise implementation.
npm install universalify
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 three or more 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!
})
MIT
FAQs
Make a callback- or promise-based function support both promises and callbacks.
The npm package universalify receives a total of 74,703,908 weekly downloads. As such, universalify popularity was classified as popular.
We found that universalify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.