I am developing an API that I would to return a promise. The returned promise is composed of several other promises, so I'm using Q.all and/or Q.allSettled. The problem I'm running into is that the code never completes (see test).
I'm expecting to be able to call then/fail/etc on what's being return from addUsers, which is a Q.all or Q.allSettlted
API:
'use strict';
var Q = require('q'),
mongoose = require('mongoose'),
User = require('../models/user'),
_ = require('underscore');
var addUsers = function (users) {
var promises = _.map(users, function (user) {
var newUser = new User(user);
var promise = Q.nbind(newUser.save, newUser);
return promise();
});
return Q.allSettled(promises);
};
module.exports.addUsers = addUsers;
Test:
'use strict';
var kraken = require('kraken-js'),
express = require('express'),
request = require('supertest'),
should = require('chai').should(),
userApi = require('../lib/userApi'),
User = require('../models/user');
describe('#userApi tests', function() {
it('should insert an array of users using the userApi addUsers method', function(done) {
var users = [];
var user1 = {
firstName: 'test1',
lastName: 'test1',
password: 'abc123',
email: 'me1@here.com',
userName: 'test1'
};
var user2 = {
firstName: 'test2',
lastName: 'test2',
password: 'abc123',
email: 'me2@here.com',
userName: 'test2'
};
var user3 = {
firstName: 'test3',
lastName: 'test3',
password: 'abc123',
email: 'me3@here.com',
userName: 'test3'
};
users.push(user1);
users.push(user2);
users.push(user3);
//call the api and handle the promise that is returned
userApi.addUsers(users)
.then(function(results) {
should.exist(results);
//other assumptions here
done();
})
.fail(function(err) {
done(err);
})
});
});