【问题标题】:How to make a synchronous workflow work using Promises in AngularJS? [duplicate]如何使用 AngularJS 中的 Promises 使同步工作流工作? [复制]
【发布时间】:2015-10-04 21:11:34
【问题描述】:

我正在 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 调用,但我不会同步获取它,因为 Promise 本质上是异步的。如果我必须实现这样的行为。我该怎么做?
第二问。无论我在我的承诺中获得什么数据,我都无法在承诺之外获得相同的价值。话虽如此。我知道这一定是因为deviceId = response.data 是一个参考。因此,当响应的范围结束时。来自deviceId 的值消失了。如何从 Promise 中获取数据?还是我应该在第一个承诺中调用另一个承诺以使其同步并确保我始终拥有数据?

【问题讨论】:

  • 你可以把var postString的代码放在then

标签: javascript angularjs promise meanjs


【解决方案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);
});

【讨论】:

  • 好吧,最后一个函数关闭了哪个括号? catch(err) { console.log('got an error'); console.log(err); }); {} 用于捕获。 );是关闭什么?所有括号都是平衡的
  • 已更正。非常感谢......这是一种使用承诺的干净方法。我认为这就是他们做出承诺的原因......
【解决方案2】:

看这个:

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时使用回调函数。回调结束时返回response的值

【讨论】:

  • 看起来正确。但随后它增加了代码行数。我认为@Jaromanda 的答案更好,因为它利用了 Promise 的属性。再次感谢。非常感谢。
  • 当然,我的回答使用了良好的编程实践。例如,您可能需要进行独立查询。这种情况下怎么办?您需要创建一个新功能吗?请记住,并不总是意味着代码行代表更高的质量。更不用说您无法决定此例程的功能名称。在我看来,她咨询给定的并返回其他陌生的方式
猜你喜欢
  • 2018-06-10
  • 2017-04-09
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 2016-09-11
  • 2013-07-14
  • 1970-01-01
  • 2012-08-11
相关资源
最近更新 更多