Freewind
Freewind

Reputation: 198388

How to debug this nodejs code?

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.

Upvotes: 2

Views: 1520

Answers (3)

Petrov
Petrov

Reputation: 4250

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();

Upvotes: 1

Hogan
Hogan

Reputation: 70538

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)

Upvotes: 1

mihai
mihai

Reputation: 38573

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

Upvotes: 0

Related Questions