1

Here is my issue, I have a javascript function in a .js file that performs some actions, gathers information, and uses a callback. The callback function is also a javascript function but resides in a .cshtml file. I am have difficulties returning a value from my .js javascript function to my .cshml javascript callback function.

Here is a little sample code...

my .js function which I would like to return a value from:

function returnVal(itemID, onCompleteCallback) {
//get vals from DB
onCompleteCallback();
}

my .cshtml script that calls the previous function and I need to get the returned value is a button click even:

updateBtn.onclick = function(e) {
if(action==1) {
    returnVal(itemID, OnCompleted);
}

I have tried 2 methods, neither has worked. I have tried returning a value within the "returnVal" function which doesn't seem to results in anything being returned. I have also tried passing a variable as type var and setting it within the returnVal function, it was my understanding that primitives are passed by value (and thus this wouldn't work) but objects are passed by reference and so I thought this would work. At any rate, neither was successful. Bellow are examples of how I have tried the aforementioned 2 methods:

Method 1:

function returnVal(itemID, onCompleteCallback) {
//get vals from DB
onCompleteCallback();
return x;
}

updateBtn.onclick = function(e) {
    if(action==1) {
        x = returnVal(itemID, OnCompleted);
    }
}

Method 2:

   function returnVal(x, itemID, onCompleteCallback) {
    //get vals from DB
    onCompleteCallback();
    }

updateBtn.onclick = function(e) {
    if(action==1) {
        var x;
        returnVal(x, itemID, OnCompleted);
    }
}

In both cases 'x' is not set. I hope I have provided enough details, any help would be greatly appreciated.

5
  • In both approaches, you are never initializing x to anything, so it would never be set. From the first approach, did you mean to do, x = onCompleteCallback(); return x;? Commented Nov 28, 2014 at 14:34
  • is x set if you dont call the callback? that callback might be breaking someting Commented Nov 28, 2014 at 14:34
  • In both cases I do not initialize x, I just initialized the var x=""; it didn't change anything, it is still not set. The problem with not calling back is I don't know if x is set because I am not waiting for it to get set so it might read that it is not set yet but that is only because the code to set it hasn't run. When I output the value of x to the console after onCompleteCallback() in the .js functin it outputs the expected value. I need the callback though. Commented Nov 28, 2014 at 14:57
  • From "//get vals from DB" I assume some asynchronous operation that is not completed when you do onCompleteCallback(); return x; you should pass your onCompleteCallback for the method stating the async operation. Commented Nov 28, 2014 at 15:07
  • you are right, there is another function call within the "returnVal" function which needs to complete before 'x' can be returned which is why I needed the callback. @chead23's answer below solved my issue though. Commented Nov 28, 2014 at 15:38

1 Answer 1

1

I think you want something like

function returnVal(itemId, onComplete){
    var data = getValueFromDatabase();
    onComplete(data);
}

updateBtn.onclick = function(e) {
    if(action==1) {
        returnVal(itemID, function(data){
            // Do something with returned value i.e. 'data'
        });
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

you are my saviour! Not sure why I didn't think of this but this is exactly what I needed! You're awesome!!

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.