4

Having what I thought should be a relatively easy problem to deal with being a major pain... I am trying to do:

a.b("param", function(data)
{
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           // No error, do stuff with data
     }
     else
     {
           // Error :(  Redo the entire thing.
     }
});

My approach to this was to try:

var theWholeThing = function() {return a.b("param", function(data)
{
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           // No error, do stuff with data
     }
     else
     {
           // Error :(  Redo the entire thing.
           theWholeThing();
     }
})};

The problem with the above is while the former did work (except didnt deal when an error occurred), the latter simply never prints the log message... its as if "theWholeThing()" call isn't working as I thought it should (call the whole thing again).

There must be something subtly wrong here, any tips?

Thanks!

0

2 Answers 2

5

First, to answer you question directly, it sounds like you forgot to actually call the function the first time. Try:

var theWholeThing = function() {return a.b("param", function(data)
{
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           // No error, do stuff with data
     }
     else
     {
           // Error :(  Redo the entire thing.
           theWholeThing();
     }
})};

theWholeThing(); // <--- Get it started.

However, this can be achieved a little more elegantly in a named IIFE (immediately invoked function expression):

// We wrap it in parentheses to make it a function expression rather than
// a function declaration.
(function theWholeThing() {
    a.b("param", function(data)
    {
         logger.debug("a.b(" + data.toString() + ")");

         if (data.err == 0)
         {
               // No error, do stuff with data
         }
         else
         {
               // Error :(  Redo the entire thing.
               theWholeThing();
         }
    });
})(); // <--- Invoke this function immediately.
Sign up to request clarification or add additional context in comments.

1 Comment

This solved the problem, thanks! (didn't realize the problem was that I was not calling the function to begin with... lol) Will accept answer as soon as So lets me (1min)
0

If you separate the methods apart and use variables to represent, things become clear. You just need to treat your a.b and anonymous function as method reference. I think this code sample can help:

var theWholeThing = a.b, //this is a reference of b method
    par = "param",
    callback = function(data){
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           console.log("no error");    
     }
     else
     {
           console.log("Error");
           // theWholeThing reference can be accessed here and you can pass your parameters along with the callback function using variable name 
           theWholeThing("param", callback); 
     }
}; //declare the callback function and store it in a variable

theWholeThing("param", callback); //invoke it

Comments

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.