Question

I need to break cycle if proccess = true, but it's undefined.

        var mapFound;
        var locations = {$addressTest};
        for (var i = 0; i < locations.length; ++i) {
            getCoordinates(
                    locations[i],
                    function(proccess) {
                    }
            )
            if(proccess) { break; }
        }
Was it helpful?

Solution

The problem seems to be basically that getCoordinates() makes an asynchronous call.

By the time the loop is over you haven't received even the first response, based on your question text, so you need to use another solution.

I mean by this that you won't be able to break the cycle, because by the time the cycle is over you still don't know if process is true.

Depending on your implementation you might want to take a look at promises. Although it might be easier to wrap the whole thing in a function that executes a callback:

function getCoords(locations, name, callback){
    for (var i = 0; i < locations.length; i++) {
        getCoordinates( locations[i], name,
            function(proccess) {
                if(process){
                    console.log("Map found in "+locations[i]);
                    callback.call();
                }
            }
        );
    }
}

getCoords({$addressTest}, {$name}, function(){
    // Place here whatever you want to do once the map is found.
    // you can get the map location by passing it as an argument for the callback.
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top