Search code examples
javascriptjquerycordovaphonegap-pluginscordova-plugins

Cordova fails in checking appAvailability inside loop.How can i make loop wait until appAvailability.check execute?


Here is the getapps function which loads application names from my website.

getapps = function (applist){   
var xmlhttp = new XMLHttpRequest();
var url = "http://mywebsite.com/"+applist;
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

    function myFunction(data) {
    var i;
    var query = data.data;
        if(query != ''){
            for(i = 0; i < query.length; i++) {
                var appinstall='';
                appAvailability.check(
                    query[i].appid,       // URI Scheme or Package Name
                    function() {  // Success callback
                         appinstall = "is available :)";
                        console.log(query[i].appid+"is available :)");
                    },
                    function() {  // Error callback
                        appinstall = "is not available :(";
                        console.log(query[i].appid+"is not available :(");
                    }
                );
                console.log(appinstall);

            }
        }
    }

}

console.log which is outside appAvailability.check function fires first for n times Within next few seconds console.log which is inside appAvailability.checkfunction fires up for n times with error undefined appid.

I Tested by removing for loop and predefining appid which worked really well without errors.

How can i resolve this by making the loop wait until appAvailability.check is completed ?


Solution

  • It's because appAvailability.check executes a success and a failure callback.

    I strongly suspect your code is making a native call in that callback which is running the callback after your callback has executed.

    You can get around this using recursion as follows:

    function getApps(appslist) {
        var xmlhttp = new XMLHttpRequest(),
            url = "http://mywebsite.com/"+applist;
    
        callback = callback || function() {};
    
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var data = JSON.parse(xmlhttp.responseText);
    
                if (data != ''){
                    checkApp(data.data);
                }
            }
        }
    
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }
    
    function checkApp(applist, callback) {
        var app = applist.push();
    
        if (!app) {
            callback();
            return;
        }
    
        appAvailability.check(
            app.appid,      // URI Scheme or Package Name
            function() {    // Success callback
                 console.log(app.appid + "is available");
    
                 // handle availability here
    
                 checkApp(applist, callback);
            },
            function() {  // Error callback
                console.log(app.appid + "is not available");
    
                // handle absence here
    
                checkApp(applist, callback);
            }
        );
    }