What is es6-promisify?
The es6-promisify package is a utility that converts callback-based functions to return Promises, making it easier to work with asynchronous code in a more modern and readable way.
What are es6-promisify's main functionalities?
Promisify a single callback-based function
This feature allows you to convert a single callback-based function, such as `fs.readFile`, into a function that returns a Promise. This makes it easier to handle asynchronous operations using modern async/await syntax or Promise chaining.
const promisify = require('es6-promisify');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
readFileAsync('example.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));
Promisify an entire object of callback-based functions
This feature allows you to promisify all functions within an object, such as the `fs` module, so that all of its methods return Promises. This is useful for working with modules that have multiple callback-based methods.
const promisify = require('es6-promisify');
const fs = require('fs');
const fsAsync = promisify(fs);
fsAsync.readFile('example.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));
Other packages similar to es6-promisify
util.promisify
The `util.promisify` function is a built-in Node.js utility that provides similar functionality to es6-promisify. It converts callback-based functions to return Promises. Since it is built into Node.js, it does not require an additional package installation. However, it is only available in Node.js versions 8.0.0 and above.
bluebird
Bluebird is a fully-featured Promise library that includes a `promisify` method. It offers more advanced features and better performance compared to es6-promisify. Bluebird is a good choice if you need additional Promise utilities and optimizations.
pify
Pify is a lightweight utility that promisifies callback-style functions. It is similar to es6-promisify but offers more customization options, such as filtering which methods to promisify and handling multiple arguments. Pify is a good alternative if you need more control over the promisification process.
es6-promisify
Converts callback-based functions to ES6/ES2015 Promises, using a boilerplate callback function.
NOTE: All-new API for Version 6.0.0; please read carefully!
Install
Install with npm
npm install es6-promisify
Example
const {promisify} = require("es6-promisify");
const fs = require("fs");
const stat = promisify(fs.stat);
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
console.error("Yikes!", err);
});
Promisify methods
const {promisify} = require("es6-promisify");
const redis = require("redis").createClient(6379, "localhost");
const client = promisify(redis.send_command.bind(redis));
client("ping").then(function (pong) {
console.log("Got", pong);
}).catch(function (err) {
console.error("Unexpected error", err);
}).then(function () {
redis.quit();
});
Handle multiple callback arguments, with named parameters
const {promisify} = require("es6-promisify");
function test(cb) {
return cb(undefined, 1, 2, 3);
}
test[promisify.argumentNames] = ["one", "two", "three"];
const multi = promisify(test);
multi().then(result => {
console.log(result);
});
Provide your own Promise implementation
const {promisify} = require("es6-promisify");
promisify.Promise = require("bluebird");
const test = promisify(cb => cb(undefined, "test"));
test().then(result => {
console.log(result);
});
Tests
Test with tape
$ npm test
Changes from v5.0.0
- Allow developer to specify a different implementations of
Promise
- No longer ships with a polyfill for
Promise
. If your environment has no native Promise
you must polyfill yourself, or set promisify.Promise
to an A+ compatible Promise
implementation. - Removed support for
settings.thisArg
: use .bind()
instead. - Removed support for
settings.multiArgs
: use named arguments instead.
Published under the MIT License.