You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

accept-promises

Accept Promises into your heart and/or functions.

1.0.1
latest
Version published
Maintainers
1
Created

accept-promises

Let them into your heart and/or functions.

Synopsis

var assert = require('assert');
var acceptPromises = require('./');
var Promise = require('bluebird');

var add = acceptPromises(function (a, b) {
  return a + b;
});

var assertIsPromise = function (it) {
  assert(Promise.is(it));
};

assertPromise(add(1, 2));

add(1, 2).then(function (three) {
  assert.equal(3, three);
});

assertPromise(
 add(1, Promise.resolve(2))).then(function (three) {
  assert.equal(3, three);
 })
);

In addition to decorating individual functions, there is also a helper for walking over an object prototype:

function Calculator () {}

Calculator.prototype = acceptPromises.mutateMethods({
  add: function (a, b) { return a + b },
  sub: function (a, b) { return a - b },
  mul: function (a, b) { return a * b },
  div: function (a, b) { return a / b }
});

Description

This module exports a function decorator that will wait for all Promise arguments to a function to settle. This allows you to avoid writing a lot of .then boilerplate in your code, and write things as though they are sync.

Acknowledgements

This is really just the bindSync decorator from z-core pulled out into it's own module, since it was the only part of z-core that I really wanted.

License

MIT

FAQs

Package last updated on 06 May 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts