【问题标题】:Chain Promises in angularJSangularJS 中的链式 Promise
【发布时间】:2013-09-30 14:00:51
【问题描述】:

这两个功能是我正在处理的音乐播放列表项目的控制器的一部分。在用户选择他的文件后,调用准备函数来检查每个文件是否可以播放。完成后调用 getIDtags 将 ID3tags 数据添加到每个文件。

       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 个函数/promises(promiseA, promiseB) 组合成一个带有链式 promise 的函数,并且在所有操作完成后仍能获得 $q.all。谢谢。

【问题讨论】:

  • 抱歉,问题不太清楚

标签: angularjs promise q chain


【解决方案1】:

因此,文件中的每个函数都将被一个接一个地调用(对于这个问题域)然后试试这个。

如果functionA返回一个promise,而functionB返回一个promise,那么functionA的promise可以用functionB的promise来解决。

function preparePlaylist(){
    var finished = $q.defer();
    // rest of function body
    $q.all(allPromises).then(function(){
        //Do whatever you need
        finished.resolve(getID3tags());
    });
    return finished.promise;

function getID3tags(){
    var finished = $q.defer();
    //rest of function body
    $q.all(ID3tagsPromises).then(function(){
        //Do whatever you need
        finished.resolve('Done both preparePlaylist and getID3tags');
    });
    return finished.promise;

preparePlaylist.then(function(response){
    console.log(response);
});

这可能需要一些调整,但它应该可以工作。不过我还没有测试过。希望能帮助到你!

参考文档:http://docs.angularjs.org/api/ng.$q

【讨论】:

  • 嘿 ryaan thanxs 但我认为我没有正确解释我的自我。 2 函数具有完全相同的结构(语法方面)。我想把它变成 1 具有链式承诺的函数。首先它检查文件,得到解析,然后得到 ID 标签,只用一个 $.each 得到解析。希望我让自己更清楚一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2015-07-27
  • 2023-03-12
  • 2015-06-18
  • 1970-01-01
相关资源
最近更新 更多