0.5.0 • Published 6 years ago

promisify-node v0.5.0

Weekly downloads
66,806
License
MIT
Repository
github
Last release
6 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-2482pm2-hookspm2-hooks2puppeteer-browserpwomppromisify-globalquantlibquantlibxl@sabbatical/generic-data-server@sabbatical/people-servicesmartcsvrequire-semverrepackersimple-blog-machine@omneedia/nodegitsass-builder@skinio/node-chartistraml-to-typescriptpm2-win-servicepm2-windows-servicepm2-windows-service-netfx-4pm2-windows-service-nosetuppm2-windows-supportpotoolsnodedevdepsrazor-clisqren-nodegitsoflowsmusplunk-slap@wize/appmaker@wize/rest-connector@wize/rest-model-generator@wize/rest-runtime@wize/rest-source@wize/soap-model-generator@wize/soap-runtime@wize/soap-source@wizeapps/sequelize-audittgomadevsalesforce-react-scriptstattooterm-flash@t2ym/web-component-testertcmtests-for-staged-filesexemplar-cliexpand-android-apieiderducktypecodergemini-reactfacilmap-serverfiletreemapgeneric-data-servergit-batch-clonegestta-basebuildgitfungit-commit-streamletsmod-loopback-graphqlmc-nodegitmulti-terminalmock-rest-apiloopback-graphql-relayloopback-graphql-serverloopback-graphql-server-fixedloopback-graphql-server-with-idskv-cachegithub-profile-messageglob-handlergnss_solutionshjson-configiaas@feizheng/next-tx-cos-sdk@fci/spajs@exeea/pm2-windows-servicemetalsmith-download-external-assetsmarcura-common-ui-helpermine-dataminxing-devtools-coregenerator-cfgenerator-rnagenerator-skimiagenerator-vca-icon-elementgrunt-vca-element-release@dvibes/loopback-graphql-relayhera-wewebinstagram-searchtagsirislogic-openstack-sdkkindle-clipping-syncjavascript-builder@elastic/nodegitparatii-contractsparatii-js
0.5.0

6 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago