0

Im working on nodejs using Q as my async module. The following invokes the first function, but nothing after that and the app doesnt return anything - it just keeps waiting.

return Q.ninvoke(exports, 'parseUser', req, null)
    .then(function(user) { console.log(1) })
    .then(exports, 'postCreateUser', user)
    .then(function() { console.log(2); })
    .then(req, 'login', user)
    .then(function() { console.log(3); })
    .then(function(user) { return res.redirect('/go'); })
    .then(function() { console.log(4); })
    .catch(function (err) { return exports.createValidationError(err, req, res, user); });

What am I doing wrong?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
user3428172
  • 147
  • 2
  • 6
  • You need to pass a function to `then()`. Try `.then(exports.postCreateUser(exports, user))` or so. – Bergi Jun 20 '14 at 10:01

1 Answers1

0

Ok, I've reconstructed your problem. The issue is that Q.ninvoke passes a callback to the method you are invoking, and you must execute that callback to continue the promise chain.

'use strict';
var Q = require('q');

var exports = module.exports;
var user = null;

exports.parseUser = function (req, nothing, cb) {
    console.log('exports.parseUser', req);
    console.log(arguments);
    user = {}; //some object
    cb(null, user);
};

exports.postCreateUser = function (user) {
    console.log('exports.postCreateUser', user);
};

var req = {
    login: function (user) {
        console.log('req.login', user);
        return user;
    }
};

var res = {
    redirect: function (route) {
        console.log('res.redirect', route);
    }
};

exports.createValidationError = function (err, req, res, user) {
    console.log('exports.createValidationError', err, req, res, user);
};


return Q.ninvoke(exports, 'parseUser', req, null)
    .then(function(user) { console.log(1); })
    .then(exports, 'postCreateUser', user)
    .then(function() { console.log(2); })
    .then(req, 'login', user)
    .then(function() { console.log(3); })
    .then(function(user) { return res.redirect('/go'); })
    .then(function() { console.log(4); })
    .catch(function (err) { return exports.createValidationError(err, req, res, user); });

And the output:

$ node q-example.js 
exports.parseUser { login: [Function] }
{ '0': { login: [Function] }, '1': null, '2': [Function] }
1
2
3
res.redirect /go
4
Nicholas Cloud
  • 1,564
  • 1
  • 13
  • 19