JSPM

@braintree/wrap-promise

3.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 651033
  • Score
    100M100P100Q182561F
  • License MIT

A small lib to return promises or use callbacks

Package Exports

  • @braintree/wrap-promise
  • @braintree/wrap-promise/dist/wrap-promise.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@braintree/wrap-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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,
});