0

For example in this case

I would like to call url1, call url2, call url3; , step by step after waiting for previous call back.

For example this is my source code

var pram={};
var url = "http://myapiserver.com/api/test";
var client = Ti.Network.createHTTPClient({
    onload : function(e) {
        Ti.API.info("first call is success!!");   
      var url2 = "http://myapiserver.com/api/test2";
       var client2 = Ti.Network.createHTTPClient({
           onload : function(e) {
              // call URL 3 here!!
              Ti.API.info("second call is success!!");
          },timeout : 3000
       });
        client2.open("GET", url);
        client2.setRequestHeader('Content-type','charset=utf-8');
        client2.send(pram2);
    },
    timeout : 3000
});

client.open("GET", url);
client.setRequestHeader('Content-type','charset=utf-8');
client.send(pram);

This source code is OK however, if you need more steps, nest structure will be deeper and deeper and difficult to maintenance.

Is there good way to clean this kind of source code??

whitebear
  • 11,200
  • 24
  • 114
  • 237
  • Possible duplicate of [Aren't promises just callbacks?](http://stackoverflow.com/questions/22539815/arent-promises-just-callbacks) – str Jan 26 '17 at 07:18
  • @str Not in the context of Appcelerator Titanium since promises are not supported (at least out of the box) – Cesar Cavazos Jan 26 '17 at 18:55

2 Answers2

2

You should take advantage of CommonJS format to have an http.js file that receives the parameters and callbacks. That should avoid repeating the creation of the HTTPClient object over and over again.

Alloy includes underscore.js out of the box. If you want to run multiple async calls at the same time you can use something like http://underscorejs.org/#after

If you can to do subsequent calls you may want to organice your code in a way that you avoid a callback hell. There is plenty of docs already about different ways to address that. Here is a Sample of an old blog post.

Cesar Cavazos
  • 271
  • 2
  • 9
0

You can try following. It is readable and easy to maintain.

var createClients = function (connections) {
    var connection = connections.shift(),
        url = connection['url'],
        param = connection['param'],
        client = Ti.Network.createHTTPClient({
            onload: function (e) {
                Ti.API.info("Call is success!");
                connections[0] && createClients(connections)
            },
            timeout: 3000
        });
    client.open("GET", url);
    client.setRequestHeader('Content-type','charset=utf-8');
    client.send(param);
};

var connections = [
    {'url': "http://myapiserver.com/api/test1", 'param': {}},
    {'url': "http://myapiserver.com/api/test2", 'param': {}},
    {'url': "http://myapiserver.com/api/test3", 'param': {}}
];

createClients(connections);

note that you will end up with empty connections


Also you can use connections to get intance of HTTPClient for each connection or extend (for example) with some specific callbacks for each connection.

Nakrar
  • 36
  • 1
  • 4