Synchronous corner functions

I have a big problem in synchronously executing corner functions. I tried promise and callback, but none of them work.

initMap().then(function(result){
    console.log("in initMap");

    getLocation().then(function(result){
        console.log("getLocation");
        if(result){
            getPlaces.getData(map,myLatlng).then(function(data){
                Array = data;
                console.log("markersArray = ", markersArray);
            }).catch(function(){
                console.log('testtesttest');
            })
        }else{
            console.log("error in getLocation");
        }

    }).catch(function(){
        console.log("getLocationError");
    })
}).catch(function(error){
    console.log("bbbbb");
})

The initMap () 'function has

{
    var defer = $q.defer();
    //Codes...
    defer.resolve(data);
    return defer.promise;
}

since the function 'getLocation' and .service'getPlaces'

However, they all run asynchronously. The console is printed as:

in initMap <-- 1
getLocation <-- 2
error in getLocation <-- 3

Number 1 should not be printed until initMap () is resolved. Since the numbers 2 and 3 should not be printed until the getLocation function has been resolved, and will verify that the result is false or true.

I'm really stumped right now.

Please, help. Any suggestions will be made. Sample code is really appreciated.

Thanks in advance.

Pawas

Edited: Below is the code for each method.

. . ? , ?

'initMap'

    var mapOptions = {
        center: myLatlng,
        zoom: 16,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var mapVar = new google.maps.Map(document.getElementById("map"), mapOptions);
    $scope.map = mapVar;

    console.log("initMap");     
    var defer = $q.defer();
    defer.resolve('initMap');
    return defer.promise;

'getLocation'

var defer = $q.defer();
var suc = false;

navigator.geolocation.getCurrentPosition(function(pos){
    myLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
    $scope.map.setCenter(myLatlng);
    suc = true;

},function(error){
    suc = false;
},{
    timeout: 12000
});
defer.resolve(suc);
return defer.promise;

'getPlaces':

Sorry, this one I can't post the code.
+4
1

, .

var defer = $q.defer(); <-- create the promise
defer.resolve('initMap'); <-- resolve it
return defer.promise; <-- returns a resolved promise

, .then . getCurrentPosition, false

var defer = $q.defer();
var suc = false;

// Here, this is a callback executed asynchronously. So the code continue to executes
navigator.geolocation.getCurrentPosition(function(pos){
    myLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
    $scope.map.setCenter(myLatlng);
    suc = true;

},function(error){
    suc = false;
},{
    timeout: 12000
});

// This is resolve with the value false from the initialization of the variable above
defer.resolve(suc);
// Always returns a resolved promise with the value false
return defer.promise;

. Google . , .

getLocation .

var defer = $q.defer();
var suc = false;

navigator.geolocation.getCurrentPosition(function(pos){
    myLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
    $scope.map.setCenter(myLatlng);
    suc = true;
    defer.resolve(suc);

},function(error){
    suc = false;
    defer.reject(suc);
},{
    timeout: 12000
});


return defer.promise;
+2

All Articles