Frage

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!

War es hilfreich?

Lösung

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.

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top