What is the difference between Q.nfcall and Q.fcall?

I am new to node.js. I am trying to understand Q.nfcall. I have the following node.js code.

function mytest() { console.log('In mytest'); return 'aaa'; } Q.nfcall(mytest) .then( function(value){ console.log(value); }); 

My expected result should be:

 In mytest aaa 

But the actual conclusion:

 In mytest 

After I changed Q.nfcall to Q.fcall in the above code, the result became what I expected:

 In mytest aaa 

Why? What is the difference between Q.nfcall and Q.fcall? Thanks.

+7
javascript q
source share
2 answers

In the Q documentation:

If you work with functions that use the Node.js pattern callback, where the callbacks are in the form of a function (err, result), Q provides several useful utility functions for converting between them. The most obvious are probably Q.nfcall and Q.nfapply

What does this mean that nfcall() expects a Node-style function(cb) , which calls cb(error, result) . Therefore, when you write:

 Q.nfcall(mytest) .then( function(value){ console.log(value); }); 

Q expects the call to mytest pass the callback with (error, value) and Q , and then calls your next callback with value . So your code should look something like this (here Plunkr ):

 function mytest(cb) { console.log('In mytest'); cb(null, 'aaa'); } Q.nfcall(mytest) .then( function(value){ console.log('value', value); }); 

You can study the nfcall () test cases to gain a deeper understanding of how this should be used.

+13
source share

The accepted answer is good, but to eliminate the difference:

  • nfapply expects an array arguments
  • nfcall expects arguments separately

Example:

 Q.nfcall(FS.readFile, "foo.txt", "utf-8"); Q.nfapply(FS.readFile, ["foo.txt", "utf-8"]); 

See https://github.com/kriskowal/q/wiki/API-Reference#qnfapplynodefunc-args

+2
source share

All Articles