同步 angularjs 函数

synchronous angularjs functions

我在使 angularjs 函数同步时遇到了很大的问题。 我已经尝试过 promise 和 callback,但是 none 有效。

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");
})

函数'initMap()'有

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

so 作为函数 'getLocation' 和 .service'getPlaces'

但是,它们都是异步完成的。 控制台打印为:

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

在解析 initMap() 之前不应打印数字 1。 因此,在解析 getLocation 之前不应打印数字 2 和 3,并检查结果是假还是真。

我现在真的走投无路了。

请帮忙。 任何建议都可以。 非常感谢示例代码。

提前致谢。

帕瓦斯

已编辑: 每个方法的代码如下。

哦对了。我在离子平台上这样做。 这会影响 angularjs 的工作方式吗? 如果出现问题我该如何解决?

'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.

你的问题是你在返回之前解决了承诺。

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;