我對整個諾言有點新,所以我可能會做一些非常錯誤的事情。如果有人能夠啓發我,歡迎所有的建議/信息。Promise.all然後(js)
所以這裏是什麼,我試圖完成(簡體字,是絕對不適合用來理解的目的)代碼:
// Get data from webservice
$scope.sendGet(id, option).then(function (response){
// Fill the model
$scope.model[option] = response.data;
}).then(function(){
if(option == $scope.PROFILES){
var p1 = new Promise((resolve, reject) => {
$scope.getX1($scope.model[option][0][0].id);
});
var p2 = new Promise((resolve, reject) => {
$scope.getX2($scope.model[option][0][0].id);
});
var p3 = new Promise((resolve, reject) => {
$scope.getX3($scope.model[option][0][0].id);
});
var p4 = new Promise((resolve, reject) => {
$scope.my_data = JSON.parse($scope.model[option][0][0].list);
});
// Execute all promises to get the data
Promise.all([p1,p2,p3,p4]).then(() => {
debugger;
// Do some validation and extra formatting on the data we just downloaded
$scope.update();
});
}
}).then(function(){
// Display the data to the user
$scope.move(option, 1, $scope.EDITING);
});
這裏預期的行爲是:
獲取數據 - >使用這個數據,使用ID從4個來源獲取數據(4個承諾) - >一旦所有的數據下載,更新一些引用,並做一些清洗 - >移動(這是一種方法,更新視圖和做一些其他的東西UI相關)
但是由於某些原因, debugger;
和$scope.update();
從未得到執行。我嘗試將它們移動到與$scope.move()
函數相同的位置,但它在Promise.all的數據已被檢索之前執行。
您P1-P4的承諾永遠不會結束,因爲你既不叫'決心()'或'拒絕() '在其中任何一個 –
你永遠不會解決你的承諾,因此他們爲什麼永遠不會執行'Promise.all'的「完成」回調。你的每一個承諾('p1-p4')都應該有一個'resolve(value)'或'reject(value)'。 – mhodges
雖然,我應該指出,因爲您使用的是angularjs,所以您應該使用['$ q service'](https://docs.angularjs.org/api/ng/service/$q)。這是完全相同的語法,基本上只需用「$ q」替換「Promise」一詞並刪除'new'關鍵字。 – mhodges