2

I'm trying to learn the "q" library with node.

$ node -v // -> v0.6.6

I'm using the latest q.js from https://github.com/kriskowal/q/blob/master/q.js now. I copied the code into a q.js file which is the sibling of my testq.js file.

The code of testq.js is:

function step1(callback) { console.log("step1"); callback("abc"); };

var Q = require("./q");

Q.fcall(step1).end();

When I run it:

node testq.js

It reports:

E:\WORKSPACE_NODE\TestNodeJs\q>node testq.js
step1

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
          ^
TypeError: undefined is not a function
    at step1 (E:\WORKSPACE_NODE\TestNodeJs\q\testq.js:1:112)
    at makePromise.<anonymous> (E:\WORKSPACE_NODE\TestNodeJs\q\q.js:541:27)
    at makePromise.promiseSend (E:\WORKSPACE_NODE\TestNodeJs\q\q.js:325:41)
    at Array.0 (E:\WORKSPACE_NODE\TestNodeJs\q\q.js:787:28)
    at EventEmitter._tickCallback (node.js:192:40)

I installed v8 debugger plugin for eclipse, debug it line by line. Unfortunately, I can't find the error start.

I'm a newbie to nodejs, please help me with debugging. E.g. what tool should I use? Or any other helpful method I should try?


UPDATE

Per Hogan's answer, the code:

function step1(callback) { console.log("step1"); };

var Q = require("./q");

Q.fcall(step1).end();

can be run successfully without any error.

But when I try:

function step1(callback) { console.log("step1"); };

var Q = require("./q");

Q.fcall(step1)
.then(function(v) {
    console.log("finished: " +v);
}, function(err){
    console.log(err);
})
.end();

It prints:

E:\WORKSPACE_NODE\TestNodeJs\q>node testq.js
step1
finished: undefined

I still want a demo that step1 will pass something to the function(v) {console.log("finished: " +v);}, where v is not an undefined.

5
  • your not passing anything to step1 - so callback is undefined Commented May 2, 2012 at 15:36
  • 1
    For debugging in general, check out node-inspector Commented May 2, 2012 at 15:43
  • 1
    Use the newest version github.com/kriskowal/q/blob/master/q.js Commented May 2, 2012 at 15:51
  • I will use the latest version of q Commented May 2, 2012 at 15:56
  • @Freewind - also see my edit for your version of q Commented May 2, 2012 at 15:56

3 Answers 3

1

Make your step1 look like this(modified as the last version):

 function step1() { console.log("step1"); return "abc" };

Then you will be happy.


Looking at the Q documentation I believe you want 'Q.node(...) or Q.ncall(...) or Q.fcall(...)

I'd read about Q some more, or if you describe what you want to do I might be able to help.


step1 takes a parameter -- a function.

But you don't define that when you call it so callback("abc"); is undefined.

Maybe it is clearer when you get rid of all the stuff that does not matter:

 function step1(callback) { console.log("step1"); callback("abc"); };

 var Q = require("q");

 Q.call(step1)
Sign up to request clarification or add additional context in comments.

4 Comments

Q.call(step1) is OK, but Q.call(step1).end() will throw that error too. Why? how to fix the step1 function to let my original Q.call()... code running?
I believe the problem is having the callback param itself. The point of Q is that you don't need callbacks as parameters.
If your version has call instead of fcall then your first comment makes sense. The way Q works is it does not "run" the functions unless end() is called. The first Q.call(step1) does not call the step1 function.
I just find the correct code, and update your answer. Thank you :)
1

The Q documentation is not very clear on this point - what Q.call/Q.fcall/Q.ncall does is to take a synchronous function (i.e. one that returns a value) and turn it into a promise.

In the docs there is a situation like this :

function step1 () {
    var result = 10;
    console.log('step1');
    callback(result);
}

function step2 (result) {
    result += 5;
    console.log('step2');
    callback(result);
}

step1 ( function(result) {
    step2 (result, function(result) {
        result += 5;
        console.log(result);
    });
});

and then the doc says you can transform it like this to get the same result

(***)
Q.fcall(step1)
  .then(step2)
  .then( function(result) {
    result += 5;
    console.log(result);
  }).end();

what I found confusing here is that the two 'step1' and 'step2' functions are not the same : the 'step1' and 'step2' he is using in the 'promisified' version really look like this :

function step1 () {
    var result = 10;
    console.log('step1');
    return result;
}

function step2 (result) {
    result += 5;
    console.log('step2');
    return result;
}

using these sychronized versions of step1 and step2, you'll see that (*) above now works


the reason all of this is interesting (for me) is that you can use the same recipe to do this

function step1 () {
    var result = 10;
    var deferred = Q.defer();
    console.log('step1');
    setTimeout(deferred.resolve, 2000, result);
    return deferred.promise;
}

function step2 (result) {
    result += 5;
    console.log('step2');
    return result;
}

​Q.fcall(step1)
    .then(step2)
    .then(function(result) {
        result += 5;
        console.log(result);
    }).end();

Comments

0

You need to use fcall instead of call. call is a reserved javascript function.

2 Comments

I don't find the fcall in my version of q, it should be an older version, and in the README of that, it uses call. I copied that part from README.
Can you post a link to your version of q then... no way to help without knowing what code you are using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.