What is pify?
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.
What are pify's main functionalities?
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);
});
Other packages similar to pify
util.promisify
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
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.
es6-promisify
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.
pify
Promisify a callback-style function
Install
$ npm install --save pify
Usage
var fs = require('fs');
var pify = require('pify');
pify(fs.readFile)('package.json', 'utf8').then(function (data) {
console.log(JSON.parse(data).name);
});
API
pify(input, [promiseModule])
Returns a promise wrapped version of the supplied function.
If the callback of the supplied function gets more than two arguments the result will be an array.
input
Type: function
Callback-style function.
promiseModule
Type: function
Custom promise module to use instead of the native one.
Check out pinkie-promise
if you need a tiny promise polyfill.
License
MIT © Sindre Sorhus