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

Build Status

es6-promisify

Converts callback-based functions to Promises, using a boilerplate callback function.

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!
try {
    const stats = await stat("example.txt");
    console.log("Got stats", stats);
} catch (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
try {
    const pong = await client.ping();
    console.log("Got", pong);
} catch (err) {
    console.error("Unexpected error", err);
} finally {
    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
const result = await multi();
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"));
const result = await test();
console.log(result); // "test", resolved using Bluebird

Tests

Test with tape

$ npm test

Published under the MIT License.

Current Tags

  • 7.0.0                                ...           latest (4 years ago)

17 Versions

  • 7.0.0                                ...           4 years ago
  • 6.1.1                                ...           5 years ago
  • 6.1.0                                ...           5 years ago
  • 6.0.2                                ...           5 years ago
  • 6.0.1                                ...           6 years ago
  • 6.0.0                                ...           7 years ago
  • 5.0.0                                ...           8 years ago
  • 4.1.0                                ...           9 years ago
  • 4.0.0                                ...           9 years ago
  • 3.0.0                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.1.1                                ...           10 years ago
  • 1.1.0                                ...           10 years ago
  • 1.0.2                                ...           10 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (16)
Dependents (3)

Copyright 2013 - present © cnpmjs.org | Home |