Converts callback-based functions to Babel or Native Promises. This module is babel port of es6-promisify.
Install with npm
npm install --save babel-promisify
"use strict";
// Declare variables
var promisify = require("babel-promisify"),
fs = require("fs"),
// Convert the stat function
stat = promisify(fs.stat);
// Now usable as a promise!
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
console.error("Yikes!", err);
});
"use strict";
// Declare variables
var promisify = require("babel-promisify"),
fs = require("fs"),
stat;
// Convert the stat function, with a custom callback
stat = promisify(fs.stat, function (err, result) {
if (err) {
console.error(err);
return this.reject("Could not stat file");
}
this.resolve(result);
});
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
// err = "Could not stat file"
});
"use strict";
// Declare variables
var promisify = require("babel-promisify"),
redis = require("redis").createClient(6379, "localhost"),
// Create a promise-based version of send_command
client = promisify(redis.send_command.bind(redis));
// Send commands to redis and get a promise back
client("ping", []).then(function (pong) {
console.log("Got", pong);
}).catch(function (err) {
console.error("Unexpected error", err);
}).then(function () {
redis.quit();
});
Test with mocha & chai
$ npm test
Published under the MIT License.