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
const fs = require('fs');
const pify = require('pify');
pify(fs.readFile)('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
});
const promiseFs = pify.all(fs);
promiseFs.readFile('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
});
API
pify(input, [promiseModule], [options])
Returns a promise wrapped version of the supplied function.
input
Type: function
Callback-style function.
pify.all(module, [promiseModule], [options])
Returns a version of the module with all its methods promisified.
module
Type: object
Module whose methods you want to promisify.
promiseModule
Type: function
Custom promise module to use instead of the native one.
Check out pinkie-promise
if you need a tiny promise polyfill.
options
multiArgs
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;
});
License
MIT © Sindre Sorhus