在使用promise链接方法时,如何确保变量不为null?

我正在使用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;

    });

};

然后链接两个诺言:


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

});

问题:我面对有关的问题var1中$scope.processData() 的var1总是具有null中值$scope.getID 的var1值undefined


慕田峪7331174
浏览 172回答 2
2回答

慕容708150

听起来这可能是闭包的问题。函数可以在定义的位置(而不是在调用的位置)访问代码的包含闭包。var1是真正的null,还是事实上的undefined?如果为undefined,则可能需要做的是使getId以var1作为参数,而不是依赖于从包含的闭包中提取引用(由于上述原因,该方法不起作用)。如果是true null,那么问题可能出在计算var1的代码中,您已经省略了它。编辑:该getID()函数看起来非常相似,但它会像这样开始:$scope.getID = function(var1) {代替这个:$scope.getID = function(var1) {调用它,您可能会执行以下操作:var var1 = somethingToGetTheValueOfVar1();$scope.getID(var1);

拉莫斯之舞

我现在面临的问题是关系到var1中$scope.processData()的var1总是具有null值范围内$scope.getID的var1值undefinedvar1作为参数添加到函数定义中:̶$̶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); }赋值语句的左侧必须是变量或属性访问器。$̶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;    });};将值返回到.then块很重要。否则,新的承诺将解决为undefined。同样重要的是要从盖帽上扔出去.catch。否则,新的承诺将从拒绝的承诺转换为已实现的承诺。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript