在执行了一些其他功能后,使用$ q执行功能?

时间:2015-03-04 14:24:06

标签: angularjs angular-promise

我可以找到的有关$q.all()的所有教程都涉及创建$q.defer()并将其传递给promises数组,然后调用$q.all(promies)

我的问题是,如果我想在执行并解决其他一些函数后执行函数怎么办?例如,我希望在DAB之后运行C

但关键是,它们是单独的功能,而不是在同一个块中

例如:

var func1 = function(){
     var deferred = $q.defer();
     // do stuff
     if (successful){
         deferred.resolve("success");
     return deferred;
}
var func2 = function(){
     var deferred = $q.defer();
     // do stuff
     if (successful){
         deferred.resolve("success");
     return deferred;
}
var func3 = function(){
     var deferred = $q.defer();
     // do stuff
     if (successful){
         deferred.resolve("success");
     return deferred;
}

    var theMainFunction = function(){
        var promises = [];
        promises.push(func1);
        promises.push(func2);
        promises.push(func3);
        $q.all(promises).then(function(){
          //do cool stuff
        });
}

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:0)

根据您给出的示例,您只需稍微更改“theMainFunction”:

var func1 = function(){
     var deferred = $q.defer();
     // do stuff
     if (successful){
         deferred.resolve("success");
     return deferred;
}
var func2 = function(){
     var deferred = $q.defer();
     // do stuff
     if (successful){
         deferred.resolve("success");
     return deferred;
}
var func3 = function(){
     var deferred = $q.defer();
     // do stuff
     if (successful){
         deferred.resolve("success");
     return deferred;
}

var theMainFunction = function(){
    var promises = [];
    promises.push(func1().promise);
    promises.push(func2().promise);
    promises.push(func3().promise);
    $q.all(promises).then(function(){
      //do cool stuff
    });
}

这是一个有效的jsFiddle:http://jsfiddle.net/pc3p9yyz/

答案 1 :(得分:-1)

下面是示例代码,附加所有承诺,然后连续执行它们,等待返回,如果所有返回都已完成,请解析最后一个。

var deferred = $q.defer();

    var broadcastArr = [];


            broadcastArr.push(func);
            broadcastArr.push(func2);
            broadcastArr.push(func3);


    var broadcastPromises = 0;
    for (var i = 0; i < broadcastArr.length; i++) {
        var returnedPromise = broadcastArr[i].callback();
        if (returnedPromise && returnedPromise.hasOwnProperty("then") === true) {

            //promise returned
            returnedPromise.then(function () {
                broadcastPromises += 1;
                if (broadcastPromises === broadcastArr.length) {
                    //All broadcasts recieved, callback now.
                    console.log("Broadcast finished");
                    deferred.resolve();
                }
            });
        } else {
            //No promise - count up
            broadcastPromises += 1;
            if (broadcastPromises === broadcastArr.length) {
                //All broadcasts recieved, callback now.
                //Maybe we just got no promises back, for take sure, do a timeout so we'll resolve.
                $timeout(function () {
                   console.log("Broadcast finished");
                    deferred.resolve();
                });
            }
        }
    }

    if (broadcastArr.length === 0) {
        //Reslove immediately
        $timeout(function () {

            console.log("No broadcasts attached");
            deferred.resolve();
        });
    }
    return deferred.promise;