亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在使用promise鏈接方法時,如何確保變量不為null?

在使用promise鏈接方法時,如何確保變量不為null?

慕田峪7331174 2021-05-14 18:12:51
我正在使用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
查看完整描述

2 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

聽起來這可能是閉包的問題。函數可以在定義的位置(而不是在調用的位置)訪問代碼的包含閉包。


var1是真正的null,還是事實上的undefined?如果為undefined,則可能需要做的是使getId以var1作為參數,而不是依賴于從包含的閉包中提取引用(由于上述原因,該方法不起作用)。如果是true null,那么問題可能出在計算var1的代碼中,您已經省略了它。


編輯:該getID()函數看起來非常相似,但它會像這樣開始:


$scope.getID = function(var1) {

代替這個:


$scope.getID = function(var1) {

調用它,您可能會執行以下操作:


var var1 = somethingToGetTheValueOfVar1();

$scope.getID(var1);


查看完整回答
反對 回復 2021-05-27
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

我現在面臨的問題是關系到var1中$scope.processData()的var1總是具有null值范圍內$scope.getID的var1值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); }

賦值語句的左側必須是變量或屬性訪問器。


$?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。否則,新的承諾將從拒絕的承諾轉換為已實現的承諾。


查看完整回答
反對 回復 2021-05-27
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號