JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4472903
  • Score
    100M100P100Q210580F
  • License MIT

Converts callback-based functions to ES6 Promises

Package Exports

  • es6-promisify

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (es6-promisify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Travis CI

es6-promisify

Converts callback-based functions to Promise-based functions.

Install

Install with npm

npm install --save es6-promisify

Example

"use strict";

// Declare variables
var promisify = require("es6-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);
});

Provide your own callback

"use strict";

// Declare variables
var promisify = require("es6-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"
});

Promisify methods

"use strict";

// Declare variables
var promisify = require("es6-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();
});

Tests

Test with nodeunit

$ npm test

Published under the MIT License.