How I can return a string from a javascript callback

时间:2016-10-20 12:41:43

标签: javascript web-services callback

How I can return a string from a javascript callback

I have two functions, main function is working on loaded. and another function is used to calling web service.

I would like to know how can JS return the string value to main function.

thanks

function thisCallJSON(webServiceURL) {
    var params = {};
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;

    gadgets.io.makeRequest(webServiceURL, function(response) 
    {
        if(response.data && response.text)
        {
            var jsondata = response.data;

            for (var key in jsondata) 
            {
                var value = jsondata[key];
                //alert("Member Name : "+value["memNm"]);
            }
        }
        else 
        {
            //alert("Member Name : Not Found !");
        }   
    }, params);
};  function main(){
var txt_string = "";
txt_string = thisCallJSON("http://192.100.1.59");

}

1 个答案:

答案 0 :(得分:0)

You can assign the value to the variable in the scope of the main function, but it won't happen before the main function is finished executing because of the event loop. Instead, you should put your code inside the callback, or better yet, look at how you would use javascript promises to accomplish this.