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 output should be:
In mytest
aaa
But the actual output is:
In mytest
After I changed Q.nfcall to Q.fcall in the code above, the output became what I expected:
In mytest
aaa
Why's that? What's difference between Q.nfcall and Q.fcall? Thanks.
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 output should be:
In mytest
aaa
But the actual output is:
In mytest
After I changed Q.nfcall to Q.fcall in the code above, the output became what I expected:
In mytest
aaa
Why's that? What's difference between Q.nfcall and Q.fcall? Thanks.
Share Improve this question asked Aug 19, 2014 at 17:35 KaiKai 3,86511 gold badges41 silver badges57 bronze badges3 Answers
Reset to default 14From the Q documentation:
If you're working with functions that make use of the Node.js callback pattern, where callbacks are in the form of function(err, result), Q provides a few useful utility functions for converting between them. The most straightforward are probably Q.nfcall and Q.nfapply
What it means is that nfcall()
expects Node-style function(cb)
which calls cb(error, result)
.
So when you write:
Q.nfcall(mytest)
.then(
function(value){
console.log(value);
});
Q
expects that mytest
calls passed callback with (error, value)
and Q
then calls your next
callback with value
.
So your code should look something like this(here is Plunkr):
function mytest(cb) {
console.log('In mytest');
cb(null, 'aaa');
}
Q.nfcall(mytest)
.then(
function(value){
console.log('value', value);
});
You can look into nfcall() test cases to go deeper how it should be used.
The accepted answer is good but to address the difference:
- nfapply expects an array of arguments
- nfcall expects arguments provided individually
For 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
While the accepted answer is a good explanation of nfcall it's not the difference between fcall and nfcall.
Both methods return a promise.
fcall returns a promise that is resolved to the return value from the function it receives.
nfcall returns a promise that is resolved (or rejected) to the value of the result of the callback that the input function should receive when called.
e.g.
return Q.nfcall(FS.readFile, "foo.txt", "utf-8");
can be translated to the following code
return new Promise((resolve, reject)=>{
FS.readFile("foo.txt","utf-8",function(err, result){
if (err)
{
reject(err);
return err;
}
resolve(result);
});
});
Where
return Q.fcall(function () {
return 10;
});
can be translated to
return new Promise((resolve,reject)=>{resolve(10);});