如何使用AngularJS中的Promise使同步工作流工作?

时间:2015-10-03 23:16:38

标签: javascript angularjs promise meanjs

我正在MEAN Stack中创建一个项目。在前端,我必须做两件事。首先使用promise从后端获取数据。并在另一个承诺中再次使用该数据。基本上是这样的。

var deviceId="";
            deviceId = $http.get(Config.apiurl+'/mobile/devices')
            .then(function(response){
                deviceId = response.data.devices[0].deviceId;
                console.log(deviceId);
                return response.data;
            }, function(response){
                console.log("Error");
                return $q.reject(response.data);
            });
            console.log(deviceId);
            var postString = "AT+CGPSINF=0 0," + lat + "," + lng + ",847.413452,20150520205855.000,100,11," + velocity + ",0.000000 OK ,CRASH=0,"+deviceId;
 $http.post(Config.apiurl + '/device/locations', postString, { headers: { 'Content-Type': undefined}})
            .then(function (response) {
                console.log("Sent the data");
                return response.data;
            }, function (response) {
                console.log("got error response");
                return $q.reject(response.data);
            });

现在有一个概念性的问题,我需要deviceId来进行第二次API调用,并且我不会同步获得它,因为promises本质上是异步的。如果我必须实施这样的行为。我该怎么办呢?
第二个问题。无论我在承诺中得到什么数据,我都无法在承诺之外获得相同的价值。话虽如此。我知道这必定是因为deviceId = response.data是一个参考。因此,当响应的范围结束时,deviceId的值将消失。如何从承诺中获取数据?或者我应该在第一个内部调用另一个承诺使其同步并确保我始终拥有数据?

2 个答案:

答案 0 :(得分:1)

看看这个:

function getMobileDevices(callback){
    $http.get(Config.apiurl+'/mobile/devices').then(function(response){
        callback(response);
    }, function(response){
        console.log("Error");
        return $q.reject(response.data);
    });
}

function getLocationDevices(callback, deviceId){
    var postString = "AT+CGPSINF=0 0," + lat + "," + lng + ",847.413452,20150520205855.000,100,11," + velocity + ",0.000000 OK ,CRASH=0,"+deviceId;
    $http.post(Config.apiurl + '/device/locations', postString, { headers: { 'Content-Type': undefined}}).then(function (response) {
        callback(response.data)
    }, function (response) {
        console.log("got error response");
        return $q.reject(response.data);
    });    
};


function go(){
    getMobileDevices(function(response){
        var deviceId = response.data.devices[0].deviceId;
        getLocationDevices(function(response){
            console.log("Response: " +  response)
        }, deviceId);
    });
}

在调用搜索http时使用回调函数。完成回调时返回响应值

答案 1 :(得分:1)

这样的事情应该有效

$http.get(Config.apiurl+'/mobile/devices')
.then(function(response) {
    deviceId = response.data.devices[0].deviceId;
    var postString = "AT+CGPSINF=0 0," + lat + "," + lng + ",847.413452,20150520205855.000,100,11," + velocity + ",0.000000 OK ,CRASH=0,"+deviceId;
    return $http.post(Config.apiurl + '/device/locations', postString, { headers: { 'Content-Type': undefined}});
}).then(function (response) {
    console.log("Sent the data");
    return response.data;
}).catch(function(err) {
    console.log('got an error');
    console.log(err);
});