how to use the value after the callback

2019-07-27 09:10发布

In my application i'm isolating the networking in a method and performing a callback to get the JSON response , how can i use it afterward ?

getJson(url, acallback);

function getJson(url, callback) {
    Ti.API.info(" im here " + url);
    var jsonObject;
    var xhr = Ti.Network.createHTTPClient();
    xhr.setTimeout(3000);
    xhr.onload = function () {
        var jsonObject = eval('(' + this.responseText + ')');
        callback.call(jsonObject)
    }
    xhr.open("GET", url);
    xhr.send();
    Ti.API.info(" passed ");
};

function acallback() {
    return this;
}

Now my questions is , how can i use the returned output afterward ?

3条回答
放我归山
2楼-- · 2019-07-27 09:33

Should look like this:

function getJson(url, callback) {
    Ti.API.info(" im here " + url );
    var jsonObject, xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(3000);
    xhr.onload = function() {
        var jsonObject = JSON.parse(this.responseText);
        callback(jsonObject)
    }
    xhr.open("GET" , url); 
    xhr.send(); 
    Ti.API.info(" passed " );
}

function acallback(json) {
      Ti.API.info("data from ajax: " + json);
}

getJson(url , acallback);

Notice that I removed the use of eval since it's bad practice to use that, as it says (here):

The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.

Also, you better use the var keyword only once per scope.


Edit

If you want to use the resulting json object from outside the scope of the callback, just define the callback in the scope where the json is needed, for example:

var obj = {
    x: 4,
    doit: function() {
        var _this = this;
        var callback = function(json) {
            alert(_this.x * json.y);
        };
        getJson(url , callback);
    }
}

The json.y part I just made up for the example of course.


2nd Edit

Alternatively, if you want to use the call option, you can do this:

function getJson(url, callback, bindto) {
    Ti.API.info(" im here " + url );
    var jsonObject, xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(3000);
    xhr.onload = function() {
        var jsonObject = JSON.parse(this.responseText);
        callback.call(bindto, jsonObject)
    }
    xhr.open("GET" , url); 
    xhr.send(); 
    Ti.API.info(" passed " );
}

var obj = {
      x: 5
}

function myCallback(json) {
      alert(this.x * json.y);
}

getJson(url, myCallback, obj);

3rd Edit

If we're on the subject, I recommend using a nice trick, which is used in Prototype, MooTools, jQuery and according to the MDN was Introduced in JavaScript 1.8.5.

Function.prototype.bind = function(scope) {
  var _function = this;

  return function() {
    return _function.apply(scope, arguments);
  }
}

You can read the tutorial where I copied to code from.

查看更多
\"骚年 ilove
3楼-- · 2019-07-27 09:42

I believe callback.call(jsonObject) should actually read callback(jsonObject). Once you have invoked an asynchronous function, the way to get its value is via a callback. So, the function you pass in as callback will get the result. Say,

function myCallback(value) {
    console.log(value);
}
查看更多
时光不老,我们不散
4楼-- · 2019-07-27 09:49

The code that needs to use the output should be placed in acallback (or placed in a function that's called from acallback).

查看更多
登录 后发表回答