2014-09-04 195 views
4

我新的Javascript和AngularJS而這一次讓我抓我的頭:/Restangular:等待承諾的決心?

前提

  • REST服務從後端提供我的數據
  • AngularJS 1.2.21和Restangular 1.4.0
  • 一個AngularJS控制器,它應該詢問服務提供的五香版本

我有什麼

這是有問題的方法:

service.getSlices = function() { 

     Restangular.all('entries').getList().then(function(entries) { 

      //some rather complex modification of the backend data go here 
      //... 

      return resultOfModification; //this is what should be returned for getSlices(); 
     }) 

     //I want the resultOfModification to be returned here 


    }; 

問題

Bascially我想在getSlices()等到應許是爲了回報解析我的resultOfModification只有當它實際計算。

其他情形
我也像返回從getSlices()承諾那麼這將提供resultOfModification。不過,我擔心我不會很好地理解這一點,並且/或者同時感到很沮喪/厭倦。

答案和任何建議,歡迎,尤其是指向良好的閱讀材料。謝謝

+1

您無法返回它在那個地方作爲實際值,因爲'Restangular'是異步的('getSlices'在調用'then'回調之前被留下)。這就是爲什麼使用'Promise'的原因。所以_correct_方法將返回Promise,並執行:'service.getSlices()。then(function(resultOfModification){});'? – 2014-09-04 19:31:54

+0

我已經成像了,這可能是不可行的,雖然我希望可能有某種方式打破這種異步。我將如何使'getSlices()'返回一個包含我的'resultOfModification'的承諾? – omilke 2014-09-04 19:36:20

回答

9

你不能在那個地方作爲實際值返回它,因爲Restangular是異步的(在調用then的回調被調用之前,功能getSlices被留下)。這就是爲什麼使用Promise

即使有可能使Restangular同步,您也不應這樣做,因爲這會阻止瀏覽器,直到數據被請求,這將是一個糟糕的用戶體驗。

您應該嘗試進入Promise,因爲它們設計爲看起來像同步代碼但行爲異步。

你需要在你的代碼改變世界的事情是增加一個returnRestangular.all

service.getSlices = function() { 
     return Restangular.all('entries').getList().then(function(entries) { 

      //some rather complex modification of the backend data go here 
      //... 

      return resultOfModification; //this is what should be returned for getSlices(); 
     }) 
    }; 

這將返回Promise是由.then調用返回。這個承諾將解決到resultOfModification,因爲這是你回報的價格。

這樣,你可以使用getSlices這樣:

service.getSlices().then(function(modifiedData) { 

    }); 

承諾可以鏈接起來:

(new Promise(function(resolve, reject){ 
    setTimeout(function() { 
     resolve("some"); 
    },200); 
    })) 
    .then(function(data) { 
    return data+' data'; 
    }) 
    .then(function(data) { 
    //here a Promise is return which will resovle later (cause of the timeout) 
    return new Promise(function(resolve, reject) { 
     setTimeout(function() { 
     resolve(data+' !!!!!!'); 
     },200); 
    }); 
    }) 
    .then(function(data) { 
    //this will have 'some data !!!!!!' 
    console.log(data); 
    }); 

這將是,如果你會寫這樣的說法相同:

var promiseA = new Promise(function(resolve, reject){ 
    setTimeout(function() { 
     resolve("some"); 
    },200); 
    }); 

    var promiseB = promiseA.then(function(data) { 
    return data+' data'; 
    }) 


    var promiseC = promiseB.then(function(data) { 
    //here a Promise is return which will resovle later (cause of the timeout) 
    return new Promise(function(resolve, reject) { 
     setTimeout(function() { 
     resolve(data+' !!!!!!'); 
     },200); 
    }); 
    }); 

    var promiseD = promiseC.then(function(data) { 
    //this will have 'some data !!!!!!' 
    console.log(data); 
    }); 
+0

這聽起來不錯。一旦我回來工作,我都渴望嘗試。非常感謝! – omilke 2014-09-05 04:56:35

+0

作品perferctly!你對鏈接承諾的解釋最有幫助:D – omilke 2014-09-05 18:32:15