0.5.0 • Published 7 years ago

promisify-node v0.5.0

Weekly downloads
66,806
License
MIT
Repository
github
Last release
7 years ago

Promisify Node

Stable: 0.5.0

Build
Status

Maintained by Tim Branyen @tbranyen.

Wraps Node modules, functions, and methods written in the Node-callback style to return Promises.

Install

npm install promisify-node

Examples

Wrap entire Node modules recursively:

var promisify = require("promisify-node");
var fs = promisify("fs");

// This function has been identified as an asynchronous function so it has
// been automatically wrapped.
fs.readFile("/etc/passwd").then(function(contents) {
  console.log(contents);
});

Wrap a single function:

var promisify = require("promisify-node");

function async(callback) {
  callback(null, true);
}

// Convert the function to return a Promise.
var wrap = promisify(async);

// Invoke the newly wrapped function.
wrap().then(function(value) {
  console.log(value === true);
});

Wrap a method on an Object:

var promisify = require("promisify-node");

var myObj = {
  myMethod: function(a, b, cb) {
    cb(a, b);
  }
};

// No need to return anything as the methods will be replaced on the object.
promisify(myObj);

// Intentionally cause a failure by passing an object and inspect the message.
myObj.myMethod({ msg: "Failure!" }, null).then(null, function(err) {
  console.log(err.msg);
});

Wrap without mutating the original:

var promisify = require("promisify-node");

var myObj = {
  myMethod: function(a, b, cb) {
    cb(a, b);
  }
};

// Store the original method to check later
var originalMethod = myObj.myMethod;

// Now store the result, since the 'true' value means it won't mutate 'myObj'.
var promisifiedObj = promisify(myObj, undefined, true);

// Intentionally cause a failure by passing an object and inspect the message.
promisifiedObj.myMethod({ msg: "Failure!" }, null).then(null, function(err) {
  console.log(err.msg);
});

// The original method is still intact
assert(myObj.myMethod === originalMethod);
assert(promisifiedObj.myMethod !== myObj.myMethod);

Tests

Run the tests after installing dependencies with:

npm test
svn-controleplatformkit-generator@fdawgs/pm2-windows-servicekonstruct-foremanlinguallo-cli@infinitebrahmanuniverse/nolb-promisinpm-bootstrap@everything-registry/sub-chunk-2482tcmtattooterm-flashwheretogoweb-component-tester-bundleweb-component-tester-bvaleweb-component-tester-custom-runnerweb-container-envworkstreams-task-indexerxcodepmwx-mobsplunk-slaptests-for-staged-filestgomadevvsts-rest@develephant/fsp@blueeast/loopback-graphql-relayirislogic-openstack-sdkjavascript-builderkindle-clipping-syncsqren-nodegittychetugboat-hitch@adobe/nodegit@alperderici/pm2-hooks@cardstack/nodegit@bryce-gibson/nodegit@clarityhub/harmony-jwt-auth@davdaarn/nodegit@davistran86/pm2-windows-serviceyet-another-captchawav2oggwc-benchwct-bstackwct-headlesswct-local-bvalewebpack-babylonjs-blenderunfort@jmnribeiro/loopback-graphql-relay@feizheng/next-tx-cos-sdk@dvibes/loopback-graphql-relay@hoangnguyen1247/pm2-hooks@igoratron/circus@flute-io/mustard-cli@fci/spajs@innomizetech/pm2-windows-service@nick92/pm2-windows-service@exeea/pm2-windows-service@elastic/nodegit@omneedia/nodegit@sabbatical/generic-data-server@sabbatical/people-servicemulti-terminalminxing-devtools-coremock-rest-apimine-datametalsmith-download-external-assetsp2oohshitgitoghlinerparatii-contractsparatii-jspaypal-payflow-helperpage-assemblerpamina-mobpakkarazor-clisass-buildersalesforce-react-scriptspwompnode-chartistnodegit-large-file-storagenodegit-large-file-storage-codelanenodegit-lfsnodegit-probablycoreynodegit-atomnodeos-nodegitnodedevdepsplatformkitpm2-hookspm2-hooks2pm2-win-servicepm2-windows-servicepm2-windows-service-netfx-4pm2-windows-service-nosetuppm2-windows-supportplugin-hookerpotoolsrepackerrequire-semverpuppeteer-browserraml-to-typescript
0.5.0

7 years ago

0.4.0

9 years ago

0.3.0

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago