7.0.0 • Published 3 years ago

es6-promisify v7.0.0

Weekly downloads
7,850,465
License
MIT
Repository
github
Last release
3 years ago

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.

@cyber-start/create-cyber-docs@cyber-start/create-cyber-library@cyber-start/create-cyber-project@cyber-start/create-cyber-server@cyber-start/create-cyber-tools@cyber-tools/cyber-order@cyber-tools/lib-clikilli8n-react-native-fast-imagespa-build-corespa-code-generatenode-scaffolding@towbe/js-tools@aleksander_ciesielski/sinusbundlersinusbundlerhomebridge-platform-raritanpdu@cashremit/cr-streamline-iconscrgt-minifiy@yushicheng/create-private-lib@cyber-tools/work-cli@cyber-work/create-cyber-work@cyber-work/lib-cli@cyber-work/spa-cli@cyber-work/work-cliwork-scripts@cyber-tools/ns-gitcloud-archive-s3@owen05/dodo-contract@frxf/frxf@radar/dydx-solonsm-ui@codewitchbella/microbundlealbum-bankakyouanydoor_tsuki@cyber-vision/create-vision-project@dolomite-exchange/v2-protocol@infinitebrahmanuniverse/nolb-es6delib@jeongbaebang/promise-flowopea-bootstraaprent-on-the-spot-frontendmanly@everything-registry/sub-chunk-1596p149-tablejzan-proxy@contrail/sdkfirst-app-lyfuciflexdeployflitter-cliflitter-uploadforgeflowerflatpak-bundlerftp-deploy-packagefs-extra-promisifyfrontupgassefalsgassefals-2infinias-doormanintelliterjaysonjayson_wrapperjamuskalimhhurleyhftkaesongkabanery-lumine-clikapeka-flow.jskenote-clikikdlabol2forlernalabrador-clilabrador-cli-patchkoa-full-authkoa-handlebarskoa-handlebars-nextkoa-hbs-babelkoa-generic-session-filekoa-generic-session-file2koa-redis-ratelimitkoa-postcsskoa-ratelimit-promiseskoa-joi-schemakoa-jsonwebtokenkonekutahomebridge-platform-pduhomebridge-digipower-pduhomebridge-raritan-pduhoodie-plugin-stripe-linkhovercardshtml-sketchapp-clihtml-sketchapp-cli-pseudo-classesimaginariahyperdb-promisehypercloudhubot-budambed-simulatormbed-connector-apimbed-creole-to-mdmbed-edge-js
7.0.0

3 years ago

6.1.1

4 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

8 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

10 years ago

0.1.0

11 years ago