alien-node-q-utils
v1.0.3
Published
Helper functions for promises with Q on NodeJS
Downloads
858
Maintainers
Readme
alien-node-q-utils
Helper functions for promises with Q on NodeJS. The functions are pure and curried with Ramda.
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 thedata
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