es6-promisify
Converts callback-based functions to ES6 Promises
Last updated 24 days ago by mikehall314 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install es6-promisify 
SYNC missed versions from official npm registry.

Travis CI

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");

// Convert the stat function
const fs = require("fs");
const 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);
});

Promisify methods

const {promisify} = require("es6-promisify");

// Create a promise-based version of send_command
const redis = require("redis").createClient(6379, "localhost");
const 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();
});

Handle multiple callback arguments, with named parameters

const {promisify} = require("es6-promisify");

function test(cb) {
    return cb(undefined, 1, 2, 3);
}

// Create promise-based version of test
test[promisify.argumentNames] = ["one", "two", "three"];
const multi = promisify(test);

// Returns named arguments
multi().then(result => {
    console.log(result); // {one: 1, two: 2, three: 3}
});

Provide your own Promise implementation

const {promisify} = require("es6-promisify");

// Now uses Bluebird
promisify.Promise = require("bluebird");

const test = promisify(cb => cb(undefined, "test"));
test().then(result => {
    console.log(result); // "test", resolved using Bluebird
});

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.

Current Tags

  • 6.0.1                                ...           latest (24 days ago)

13 Versions

  • 6.0.1                                ...           24 days ago
  • 6.0.0                                ...           9 months ago
  • 5.0.0                                ...           2 years ago
  • 4.1.0                                ...           2 years ago
  • 4.0.0                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.1                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 243
This Week 16,855
This Month 64,498
Last Day 8,664
Last Week 35,555
Last Month 135,132
Dependencies (0)
None
Dev Dependencies (7)
Dependents (596)

Copyright 2014 - 2016 © taobao.org |