這兩個功能是我正在處理的音樂播放列表項目的控制器的一部分。在用戶選擇他的文件後,調用準備功能來檢查每個文件是否有效。完成後,調用getIDtags將ID3tags數據添加到每個文件。angularJS鏈承諾
var preparePlaylist = function (files){
var allPromises = [],
currentSongsListLength = $scope.songsList.length || 0;
$.each(files, function (index){
var file = files[index];
file.index = index + currentSongsListLength;
var promiseA = FileValidator2.check(file);
allPromises.push(promiseA);
promiseA
.then (function (file){
SharedService.addSong(file.index, file);
});
});
$q
.all(allPromises)
.then(function() {
console.log('Check Resolved!');
console.log('SharedService updated.');
getID3tags();
});
};
var getID3tags = function(){
var ID3tagsPromises = [];
$.each($scope.songsList, function(index){
var promiseB = ID3tags.getTags($scope.songsList[index]);
ID3tagsPromises.push(promiseB);
promiseB
.then(function (file){
SharedService.addSong(file.index, file);
});
});
$q
.all(ID3tagsPromises)
.then(function() {
console.log('ID3tags Resolved!');
console.log('SharedService updated.');
});
};
如何將2個功能/承諾(promiseA,promiseB)合併成鏈式承諾一個功能,仍然可以得到$ q.all當一切都done.Thanxs。
抱歉,但問題是,還不是很清楚 –