babel-promisify
Converts callback-based functions to Babel or Native Promises. This module is babel port of es6-promisify.
Install
Install with npm
npm install --save babel-promisify
Example
"use strict";
var promisify = require("babel-promisify"),
fs = require("fs"),
stat = promisify(fs.stat);
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
console.error("Yikes!", err);
});
Provide your own callback
"use strict";
var promisify = require("babel-promisify"),
fs = require("fs"),
stat;
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) {
});
Promisify methods
"use strict";
var promisify = require("babel-promisify"),
redis = require("redis").createClient(6379, "localhost"),
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();
});
Tests
Test with mocha & chai
$ npm test
Published under the MIT License.