2

I have written a small code-snippet it prints the output but its not returning any value from the callback()

Can you please help me where am I making a mistake `

function helloworld(string,callback){
  //callback(string){
        if ( string == "Tim"){
          callback('null',string+" "+"Little");
        } else if ( string == "Jason"){
          callback('null',string+"Right") ;
        } else {
          callback ('error : No rule is defined for '+string,string);
        }
    //  };
}


function invoke_callback(input){
      helloworld(input,
          function(err,name){
              if (err != 'null'){
                console.log(err);
                return(err);
              }
              console.log(name);
              return(name);
          }
      )
}

console.log(invoke_callback("Tim"));

`

Its output is - Tim Little undefined

I am expecting - Tim Little Tim Little

Arnab
  • 140
  • 1
  • 4

3 Answers3

0

You are missing things. The last console.log is printing what invoke_callback returns, which is nothing. That's why the undefined. For getting what you want, you have to return the result of helloworld method. But this method doesn't return anything either. So you need to return callback result. And you will get it then. Here it is how it looked like:

function helloworld(string,callback){
  //callback(string){
        if ( string == "Tim"){
          return callback('null',string+" "+"Little");
        } else if ( string == "Jason"){
          return callback('null',string+"Right") ;
        } else {
          return callback ('error : No rule is defined for '+string,string);
        }
    //  };
}


function invoke_callback(input){
      return helloworld(input,
          function(err,name){
              if (err != 'null'){
                console.log(err);
                return(err);
              }
              console.log(name);
              return(name);
          }
      )
}

console.log(invoke_callback("Tim"));
David Vicente
  • 3,091
  • 1
  • 17
  • 27
  • 1
    Hi David Vicente, This was a really a helpful tip .. I actually messed up in the flow of callback and got my returns entangled up. I got them fixed now. Especially the flow is invoke_callback() --> helloworld() --> callback(). So the return should also be from callback() -->helloworld() --> invoke_callback(). – Arnab Feb 19 '18 at 18:01
  • I'm glad it helped – David Vicente Feb 19 '18 at 19:19
0

The invoke_callback does not return anything, so the output is corect!

If you think that return(name); is return value to invoke_callback call then you are wrong.

return(name); is returning the name to the callback in helloworld function

If you do this:

...
if ( string == "Tim"){
    // here is where your `return name;` ends up
    var name = callback('null',string+" "+"Little"); 
    console.log(name);
} else if ( string == "Jason"){
...

then you get what you want

And this:

return(name); 

is incorrect, it should be

return name; // without parentheses
Molda
  • 5,619
  • 2
  • 23
  • 39
  • Molda, I wanted to return from the method() and its a very much of a doable and anticipated task for any method. And return (name) or return name might not create any difference. They will share the same outcomes. – Arnab Feb 19 '18 at 18:06
0

What you are doing wrong here is that you are actually returning inside of helloworld function in actual you should return outside of this function and inside of invoke_callback function by using some variable like this..

var SomeName;

function invoke_callback(input){
      helloworld(input,
          function(err,name){
              if (err != 'null'){
                console.log(err);
                return(err);
              }
              console.log(name);
              SomeName = name;
              
          }
      )
      return(SomeName);
}

console.log(invoke_callback("Tim"));
Kunal Pal
  • 545
  • 8
  • 21
  • We are using var SomeName as a global variable and making it accessible from anywhere in the code. A return will be much safer approach. just we need to return from the helloworld() method to invoke_callback(). – Arnab Feb 19 '18 at 18:08
  • I did not get you. – Kunal Pal Mar 03 '18 at 10:07