2
var str ='';
function myFormatter(cellvalue, options, rowObject) {

    if(rowObject.name== 'test'){

        $.getJSON(loadUrl, function(jsnResponse) {

            str=jsnResponse.address;
        }); 
    }else{
        str = 'No value'; 
    }
    return str;
    }

This is a function that i have written,the problem is that when this function is called, it first executes return str line and returns blank and then the if statement is executed and again the correct value is returned.

Any help will be appreciated.Thanks

Amin Jafari
  • 7,157
  • 2
  • 18
  • 43
Monika
  • 37
  • 3
  • 3
    Welcome to the world of Ajax. – Felix Kling Aug 21 '14 at 05:42
  • 2
    @FelixKling I'd be interested in finding out which question is the most often linked as a duplicate. I think you might be the winner. http://stackoverflow.com/questions/linked/14220321?lq=1 – user229044 Aug 21 '14 at 05:45
  • 1
    Instead of `str=jsnResponse.address;` use `return str=jsnResponse.address;` and remove last one – Manwal Aug 21 '14 at 05:45
  • @meagar: uh wow, that's a lot of questions! Yeah, mine is currently the top one in the list of "frequent" questions. What can I say, people seem to like it :) although I think http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron is doing a better job in explaining what asynchronous means. – Felix Kling Aug 21 '14 at 05:50
  • @Manwal: no, that won't solve the problem. – Felix Kling Aug 21 '14 at 05:57

3 Answers3

2

Try this:

var str ='';
function myFormatter(cellvalue, options, rowObject) {

    if(rowObject.name== 'test'){

        $.getJSON(loadUrl, function(jsnResponse) {

            return str=jsnResponse.address;
        }); 
    }else{
        return str = 'No value'; 
    }
 }
Mayank Sharma
  • 844
  • 7
  • 21
0

The function is calling a promise. Instead of returning from the function that calls the promise, subscribe to the promise an execute your logic when the promise callback is invoked. You are already populating your variable within the callback, so execute the rest of your logic there:

$.getJSON(loadUrl, function(jsnResponse) {
    str=jsnResponse.address;
    DoStuff(str);
}); 
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
0

This happens because getJSON is asynchronous function. You may use $.ajax with async: false instead of getJSON, like:

$.ajax({
  dataType: "json",
  url: loadUrl,
  async: false,
  success: function(response) { ... }
});

But I would suggest to use callback instead, like:

var str ='';
function myFormatter(cellvalue, options, rowObject, callback) {
if(rowObject.name== 'test'){
        $.getJSON(loadUrl, function(jsnResponse) {
            if(typeof(callback) === 'function'){
                callback(jsnResponse.address);
            }
        }); 
    } else {
        str = 'No value'; 
    }
    return str;
}

And then call it like:

myFormatter('...', '...', rowObject, function(str) {
    // Here you will receive the value asynchronously.
});
Htmlin.com
  • 121
  • 3
  • Thanks.How can I assign the value returned by this function to a jqgrid column?I want to apply this method as a formatter for jqgrid. – Monika Aug 21 '14 at 11:45