4

How can I get the value of var result in this code?

I know this is a basic problem but I'm looking for the solution since 3 days. Can you give me any suggestion please?

function foo(myCallback){
}

function bar() {
    var result = foo(function(){
        var result = "hello"; 
        return result;
    });
}

var showResult = bar();
alert(showResult);
2
  • You can get it in the callback Commented Apr 4, 2016 at 10:59
  • function foo(myCallback) { } function bar(cb) { foo(function() { var result = "hello"; cb(result); }); } bar(function(res) { alert(res); }); Commented Apr 4, 2016 at 11:02

3 Answers 3

6

You need to call the callback and return the value of it and inside your bar function, you need to return the result as well

function foo(myCallback){
    // return the value of the call myCallback()
    return myCallback();
}

function bar(){
    var result = foo(function(){
        var result = "hello"; 
        return result;
    });
    // return the result
    return result;
}
var showResult = bar();
alert(showResult);

A bit simplified it could be

function foo(myCallback){
    return myCallback();
}

function bar(){
    return foo(function(){
        return "hello"; 
    });
}
var showResult = bar();
alert(showResult);

Sign up to request clarification or add additional context in comments.

8 Comments

You could even simply replace the initial var result declaration with return itself. Actually, in this case, all var result = ...; return result; could be replaced with return ...
Thank you very much Nina Scholz this is the good answer ! :-) It is good to see your first answer which is the same as my question. And then I can see a simplified version. Thank you !
@Nina Scholz I'm sorry but the system tells me I haven't enough reputation to be able to vote for your answer ...
@zm455 You should be able to vote on your own question, and you can most certainly mark an answer to your own question as correct :)
Hello @somethingThere stackoverflow's system tells me I can't vote for my own post. I would be more than happy to be able to vote for people who helped me. I just need 2 people votes for my question and I will be 15 reputation which is the score I need to be able to accept answer and vote
|
2

You are missing the return statements. It isn't clear what you want to return.

It works like this:

function foo(myCallback){
  return myCallback();
}

function bar(){
    var result = foo(function(){
        var result = "hello"; 
        return result;
    });
  return result;
}
var showResult = bar();
alert(showResult);

Comments

0

You are stuck using callbacks, but fortunately you can pass parameters in your callback functions:

// define your functions
function foo(myCallback){
    myCallback();
}

function bar(callback){
    var result = foo(function(){
        var result = "hello"; 
        callback(result);
    });
}

// now run it
bar(function(showResult){
    alert(showResult);
});

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.