【问题标题】:How to make sure that the variable is not null while using promise chaining approach?使用承诺链接方法时如何确保变量不为空?
【发布时间】:2019-09-29 05:50:09
【问题描述】:

我使用init如下

$scope.init = function () {
    $scope.getAllDetails();
};

$scope.getAllDetails= function () {
    $http({
        method: "GET",
        url: "api/endpoint"
    }).then(function mySuccess(response) {
            $scope.processData();
        }, function myError(response) {
            console.log(response.statusText);
        }
    );
};

$scope.processData() = function() {
//Logic to calculate var1
// var1 is then passed to $scope.getID to make call to Service1.getId(var1)

}

我的第一个函数返回一个承诺:

$scope.getID = function() {
    return Service1.getId("abc").then(function(response){
    // In above line. Instead of manually passing `abc` I want to 
    //pass it as variable var1  
    // The desired call should be as below. 
    //But var1 is having null here
  //return Service1.getId(var1).then(function(response){
        $scope.genewtId = response.data[0].Id;
        console.log($scope.genewtId);
        return response.data[0].Id;
    }, function(error){
        console.log(error.statusText);
        throw error;
    });
};

第二个函数返回一个promise并接受一个参数:

$scope.getDetails = function(id) {
    var genewtID = id || $scope.genewtId;
    return Service2.getDetails(genewtId).then(function(response){
        $scope.data1 = response.data;
        console.log($scope.data1.toString());
        return response.data;
    }, function(error){
        console.log(error.statusText);
        throw error;
    });
};

然后chaining两个promise:

var promise = $scope.getId();

var promise2 = promise.then(function(id) {
                   return $scope.getDetails(id);
               });

var promise2.then(function(data) {
     console.log(data);
}).catch(function(error) {
     console.log(error);
});

问题: 我面临的问题与$scope.processData() 中的var1 有关 var1$scope.getID 中始终具有 nullvar1 的值为 undefined

【问题讨论】:

标签: javascript angularjs angularjs-scope angular-promise angularjs-service


【解决方案1】:

听起来像这样可能是闭包的问题。函数将可以访问代码的包含闭包定义,而不是调用

var1 是真的null,还是实际上是undefined?如果是undefined,您可能需要做的是让 getId 将 var1 作为 argument 而不是依赖于从包含闭包中提取引用(由于上述原因,这将不起作用)。如果真的是null,那么问题可能出在计算var1 的代码上,而你已经省略了。

编辑:getID() 函数看起来非常相似,但它会像这样开始:

$scope.getID = function(var1) {

而不是这个:

$scope.getID = function(var1) {

要调用它,您可能会这样做:

var var1 = somethingToGetTheValueOfVar1();
$scope.getID(var1);

【讨论】:

  • var1 的值为 undefined
  • 您能否建议更新后的getId 的外观?
  • 添加到我的原始答案中。但是,让 getId 进行论证将意味着您必须改变最后链接您的承诺的方式。如果我能提供一些关于如何做到这一点的建议,请告诉我。
【解决方案2】:

我面临的问题与$scope.processData() 中的var1 有关var1$scope.getID 中始终具有nullvar1 值是undefined

var1作为参数添加到函数定义中:

̶$̶s̶c̶o̶p̶e̶.̶g̶e̶t̶I̶D̶ ̶=̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶)̶ ̶{̶
$scope.getID = function(var1) {
    //return Service1.getId("abc").then(function(response){
    // In above line. Instead of manually passing `abc` I want to 
    //pass it as variable var1  
    // The desired call should be as below. 
    //But var1 is having null here
    return Service1.getId(var1).then(function(response){
        $scope.genewtId = response.data[0].Id;
        console.log($scope.genewtId);
        return response.data[0].Id;
    }, function(error){
        console.log(error.statusText);
        throw error;
    });
};

我正在尝试如下调用该函数,但它不起作用

$scope.processData() = function() { ... $scope.getID(var1); }

assignment statement 的左侧必须是变量或属性访问器。

$̶s̶c̶o̶p̶e̶.̶p̶r̶o̶c̶e̶s̶s̶D̶a̶t̶a̶(̶)̶ ̶=̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶)̶ ̶{̶ ̶
$scope.processData = function(data) { 
    //... 
    var var1 //= something
    return $scope.getID(var1); 
}

那么一定要正确链接:

$scope.getAllDetails= function () {
    return $http({
        method: "GET",
        url: "api/endpoint"
    }).then(function mySuccess(response) {
        var data = response.data;
        return $scope.processData(data);
    }, function myError(response) {
        console.log(response.statusText);
        //IMPORTANT RE-THROW error
        throw response;
    });
};

return 值转换为.then 块很重要。否则,新的 Promise 将解析为 undefined

.catch 块到throw 也很重要。否则,新的 Promise 将从被拒绝的 Promise转换为已履行的 Promise。

有关详细信息,请参阅

【讨论】:

  • 我正在尝试按以下方式调用该函数,但它不起作用$scope.processData() = function() { ... $scope.getID(var1); }
猜你喜欢
  • 2018-03-14
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2019-05-24
  • 2015-09-26
相关资源
最近更新 更多