Skip to content
Small module to help support APIs that return a promise or use a callback.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test Log errors that occur in consumer provided callback (#5) Apr 17, 2019
.eslintrc Create Wrap-Promise (#1) Mar 8, 2017
.gitignore
.nvmrc
.travis.yml Add travis.yml Jun 27, 2019
CHANGELOG.md Update CHANGELOG to v2.0.0 Apr 17, 2019
LICENSE Initial commit Mar 7, 2017
README.md Add method to wrap the prototype (#2) Mar 24, 2017
package-lock.json
package.json Update dev deps Aug 28, 2019
wrap-promise.js Update dependencies Apr 11, 2019

README.md

wrap-promise

Small module to help support APIs that return a promise or use a callback.

Example

// my-method.js
var wrapPromise = require('wrap-promise');

function myMethod (arg) {
  return new Promise(function (resolve, reject) {
    doSomethingAsync(arg, function (err, response) {
      if (err) {
        reject(err);
        return;
      }

      resolve(response);
    });
  });
}

module.exports = wrapPromise(myMethod);

// my-app.js
var myMethod = require('./my-method');

myMethod('foo').then(function (response) {
  // do something with response
}).catch(function (err) {
  // handle error
});

myMethod('foo', function (err, response) {
  if (err) {
    // handle error
    return;
  }

  // do something with response
});

Wrap All Methods on the Prototype

function MyObject() {}

MyObject.prototype.myAsyncMethod = function () {
  return new Promise (function (resolve, reject) {
    //
  });
};

MyObject.prototype.myOtherAsyncMethod = function () {
  return new Promise (function (resolve, reject) {
    //
  });
};

module.exports = wrapPromise.wrapPrototype(MyObject);

Static methods, sync methods on the prototype (though if you pass a function as the last argument of your sync method, you will get an error), and non-function properties on the prototype are ignored.

If there are certain methods you want ignored, you can pass an ignoreMethods array.

module.exports = wrapPromise.wrapPrototype(MyObject, {
  ignoreMethods: ['myMethodOnThePrototypeIDoNotWantTransformed']
});

By default, wrapPrototype ignores methods that begin with an underscore. You can override this behavior by passing: transformPrivateMethods: true

module.exports = wrapPromise.wrapPrototype(MyObject, {
  transformPrivateMethods: true
});
You can’t perform that action at this time.