alien-node-q-utils

Helper functions for promises with Q on NodeJS

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
alien-node-q-utils
1.0.32 months ago8 years agoMinified + gzip package size for alien-node-q-utils in KB

Readme

alien-node-q-utils
Helper functions for promises with Q on NodeJS. The functions are pure and curried with Ramda.
Build Status Coverage Status npm version Dependency Status

Install

$ npm install alien-node-q-utils --save

Run the specs
$ npm test

Methods

rejectedPromise

A pre-rejected promise that throws the rejection into an expected catch block.
it('should pre-reject a promise', function(done) {
  rejectedPromise('foo')
    .catch(function(err) {
      expect(err).toBe('foo');
      done();
    })
});

resolvedPromise

A pre-resolved promise that passes the resolution into the expected then block.
it('should pre-resolve a promise', function(done) {
  resolvedPromise('foo')
    .then(function(data) {
      expect(data).toBe('foo');
      done();
    })
});

rejectOnErrorOrResolve

Given a function signature of fn(deferred, err, data) this will reject deferred if err is provided, otherwise resolve deferred with data. - Don't forget this function is curried, so the arity must be recognized. (If you omit the data param, you will get a function that accepts only the data param.)
var mockPromiseNoError = function(resolveWith) {
  var deferred = Q.defer();
  rejectOnErrorOrResolve(deferred, undefined, resolveWith);
  return deferred.promise;
};

var mockPromiseWithError = function(err) {
  var deferred = Q.defer();
  rejectOnErrorOrResolve(deferred, err, undefined);
  return deferred.promise;
};

it('should resolve if no error is provided', function(done) {
  var deferred = Q.defer();
  mockPromiseNoError('foo')
    .then(function(data) {
      expect(data).toBe('foo');
      done();
    });
});

it('should reject if error is provided', function(done) {
  var deferred = Q.defer();
  mockPromiseWithError('foo')
    .catch(function(err) {
      expect(err).toBe('foo');
      done();
    });
});

TODO

- Need specs and descriptions for new mapP methods