user555303 user555303 - 1 year ago 63
Node.js Question

Trying to understand node.js callback when the function normally doesn't expect any parameters?

I am trying to work with node.js and node-java and trying to get my head wrapped around some concepts, and in particular how to write async method calls.

I think that, for a function in Java, myclass.x():

[In Java]:

Z = myclass.x(abc);


[In node.js/node-java]:

myclass.x(abc, function(err,data) {
Z = data;});

In other words, the myclass.x function gets evaluated using the parameter abc, and if no error, then the result goes into "data" which is then assigned to Z.

Is that correct?

Here's the thing (or one of the things) that I am confused about.

What happens if the function myclass.x() doesn't take any parameters?

In other words, it is normally (in Java) just called like:

Z = myclass.x();

If that is the case, how should the node.js code look?

myclass.x(, function(err,data) {
Z = data;});

doesn't seem right, but:

myclass.x( function(err,data) {
Z = data;});

also doesn't seem correct.

So what is the correct way to code the node.js code in this case?

Thanks in advance!!


EDIT 1: Per comments, I'm adding the specific code I'm working with is the last couple of commented out lines from this other question at:

node.js and node-java: What is equivalent node.js code for this java code?

These are the lines (commented out in that other question):

var MyFactoryImplClass = java.import("");

var result = myFactoryImplClass.newPepRequest(newSubject, requestACTIONString ,requestRESOURCEString , envBuilt)

I tried to make the last line use an async call:

MyFactoryImplClass.getPepRequestFactory( function(err,data) {
javaLangSystem.out.printlnSync("Finished doing MyFactoryImplClass.getPepRequestFactory() and stored it in pepReqF1 =[" + pepReqF1 + "]");

But the output was showing the value of that pepReqF1 as "undefined".

Answer Source

Making my comment into an answer...

If the issue you're experiencing is that Z does not have the value you want when you are examining it, then that is probably because of a timing issue. Asynchronous callbacks happen at some unknown time in the future while the rest of your code continues to run. Because of that, the only place you can reliably use the result passed to the asynchronous callback is inside the callback itself or in some function you would call from that function and pass it the value.

So, if your .x() method calls it's callback asynchronously, then:

var Z;
myclass.x( function(err,data) {
    // use the err and data arguments here inside the callback
    Z = data;

console.log(Z);    // outputs undefined

// you can't access Z here.  Even when assigned 
// to higher scoped variables because the callback has not yet
// been called when this code executes

You can see this is a little more clearly by understanding the sequencing

someAsyncFucntion(function() {

This will produce a log of:


Showing you that the async callback happens some time in the future, after the rest of your sequential code has executed.

Java, on the other hand, primarily uses blocking I/O (the function doesn't return until the I/O operation is copmlete) so you don't usually have this asynchronous behavior that is standard practice in node.js. Note: I believe there are some asynchronous capabilities in Java, but that isn't the typical way things are done and in node.js, it is the typical ways things are done.

This creates a bit of an architectural mismatch if you're trying to port code that uses I/O from environment from another because the structure has to be redone in order to work properly in a node.js environment.