Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Package description
The pify npm package is a utility module that converts callback-based functions or methods to return Promises. This is particularly useful when working with older Node.js or JavaScript libraries that do not natively support Promises, allowing developers to write cleaner, more modern asynchronous code using async/await or .then() chaining.
Promisifying a single function
This code sample demonstrates how to promisify Node.js's fs.readFile function using pify. The resulting readFileAsync function returns a Promise that resolves with the file's contents or rejects with an error.
const pify = require('pify');
const fs = require('fs');
const readFileAsync = pify(fs.readFile);
readFileAsync('file.txt', 'utf8').then(data => {
console.log(data);
}).catch(error => {
console.error(error);
});
Promisifying an entire module
This code sample shows how to promisify all the functions of the fs module. After promisification, methods like fs.readFile return Promises.
const pify = require('pify');
const fs = pify(require('fs'));
fs.readFile('file.txt', 'utf8').then(data => {
console.log(data);
}).catch(error => {
console.error(error);
});
Custom promisification options
This code sample illustrates how to use pify with custom options. The 'exclude' option prevents certain functions from being promisified, while 'multiArgs' allows the promise to resolve with an array of values if the original callback returns multiple arguments.
const pify = require('pify');
const someModule = require('some-module');
const promisifiedModule = pify(someModule, {
exclude: ['nonAsyncFunction'],
multiArgs: true
});
promisifiedModule.someFunction().then(result => {
const [firstResult, secondResult] = result;
console.log(firstResult, secondResult);
});
Built into Node.js, util.promisify is a core method that converts a callback-based function into a Promise-based one. It is similar to pify but does not offer the same level of customization, such as excluding functions or handling multiple callback arguments.
Bluebird is a comprehensive promise library that includes a promisify and promisifyAll method, which are similar to pify's functionality. Bluebird promises offer additional features such as cancellation, progress, and long stack traces, which pify does not.
The es6-promisify package is another alternative that converts callback-based functions into Promises. It is similar to pify but with a slightly different API and does not provide as many options for customization.
Readme
Promisify a callback-style function
$ npm install --save pify
const fs = require('fs');
const pify = require('pify');
pify(fs.readFile)('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
//=> 'pify'
});
// promisify all methods in a module
const promiseFs = pify.all(fs);
promiseFs.readFile('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
//=> 'pify'
});
Returns a promise wrapped version of the supplied function.
Type: function
Callback-style function.
Returns a version of the module with all its methods promisified.
Type: object
Module whose methods you want to promisify.
Type: function
Custom promise module to use instead of the native one.
Check out pinkie-promise
if you need a tiny promise polyfill.
Type: boolean
Default: false
By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like request
that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument.
const request = require('request');
const pify = require('pify');
pify(request, {multiArgs: true})('http://sindresorhus.com').then(result => {
const [httpResponse, body] = result;
});
MIT © Sindre Sorhus
FAQs
Promisify a callback-style function
The npm package pify receives a total of 65,764,613 weekly downloads. As such, pify popularity was classified as popular.
We found that pify 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.