Search code examples
javascripttitanium

Is it possible to make event/callback by myself? javascript


I would like to call a function after two http call finished.

So I need to wait until two http call finished.

This code is titanium however, structure is common for javascript-like langs.

I made some code for now,,, however I am not sure how to handle original callback....

var flg1 = false;
var flg2 = false;

function(callback if (flg1 && flg2)){console.log"Now two flg is changedd!!!";}


// first http call
var pram={};
var url = "http://myapiserver.com/api/test";
var client = Ti.Network.createHTTPClient({
    onload : function(e) {
        Ti.API.info("first call is success!!");   
       flg1 = true; //change flg to true
       });
    },
    timeout : 3000
});

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


// second http call
var pram2={};
var url2 = "http://myapiserver.com/api/test2";
var client2 = Ti.Network.createHTTPClient({
    onload : function(e) {
        Ti.API.info("second call is success!!");   
          flg2 = true; //change flg to true
       });
    },
    timeout : 3000
});

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

Solution

  • I'm not familiar with Titanium, but I think it supports promises and you should try to use the Promise.all() method to execute something when both calls have concluded.

    Either way, you can wrap everything in promises yourself, as such:

    var p1 = new Promise(function (resolve, reject) {
        var pram={};
        var url = "http://myapiserver.com/api/test";
        var client = Ti.Network.createHTTPClient({
            onload : function(e) {
                // extract data from e,
                data = e
                resolve(data)
            },
            onerror: function (error) {
                reject(error)
            },
            timeout : 3000
        });
    
        client.open("GET", url);
        client.setRequestHeader('Content-type','charset=utf-8');
        client.send(pram);
    })
    
    
    // second http call
    var p2 = new Promise(function (resolve, reject) {
        var pram2={};
        var url2 = "http://myapiserver.com/api/test2";
        var client2 = Ti.Network.createHTTPClient({
            onload : function(e) {
                // extract data from e,
                data = e
                resolve(data)
            },
            onerror: function (error) {
                reject(error)
            },
            timeout : 3000
        });
    
        client2.open("GET", url);
        client2.setRequestHeader('Content-type','charset=utf-8');
        client2.send(pram2);
    }
    
    Promise.all([p1, p2]).then(function (dataComingFromP1, dataComingFromP2) {
         // magik
    })
    

    If you need to polyfill Promises as they're not supported by Titanium by default (again, I'm not a Titanium user), there are lot of libraries on npm you can use.