解决angularjs中的多个promise [英] resolve multiple promises in angularjs

查看:148
本文介绍了解决angularjs中的多个promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工厂,它从API获取数据并将每个数据存储到SQLLite数据库.

Here is my factory which gets data from an API and stores each data to a SQLLite database.

team.factory('dataSync', function($q,$http,$timeout,$cordovaSQLite ){
    return {
        getData:function(){
            var q = $q.defer();
             $http.get(api+'/sync/').then(function(response){
                q.resolve(response);
            },function(error){
                q.reject();
            })
            return q.promise;

        },

        saveData:function(){
            var q= $q.defer();

            this.getData().then(function(result){
                var data= result.data;
                var sharing = data.sharing;
                var help = data.help;
                var message = data.message;
                var questions = data.question;

                var promises = [];

                angular.forEach(sharing, function(value, index) { 
                        console.log(value);
                    var sharingsql="INSERT INTO sharing (id,content_order,content ,last_modified)VALUES(?,?,?,?)";

                     $cordovaSQLite.execute(db,sharingsql,[value.id,value.content_order,value.content,value.last_modified]).then(function(result){
                        console.log(result.insertId);
                        //q.resolve(true);
                     },function(error){
                        console.log(error.message);
                     })
                });
                angular.forEach(help, function(value, index) { 
                    console.log(value.message);
                    var helpsql="INSERT INTO help (id,message,message_position,last_modified)VALUES(?,?,?,?)";

                     $cordovaSQLite.execute(db,helpsql,[value.id,value.message,value.message_position,value.last_modified]).then(function(result){
                        console.log(result.insertId);
                     },function(error){
                        console.log(error.message);
                     })
                });

                    angular.forEach(message, function(value, index) { 
                    var messagesql="INSERT INTO messages (id,message,message_position,last_modified_date)VALUES(?,?,?,?)";

                     $cordovaSQLite.execute(db,messagesql,[value.id,value.message,value.message_position,value.last_modified]).then(function(result){
                        console.log(result.insertId);
                     },function(error){
                        console.log(error.message);
                     })
                });

                    angular.forEach(questions, function(value, index) { 
                    console.log(value.id+' '+index);
                    var questionsql="INSERT INTO questions (id,question_status,questions,question_order,last_modified)VALUES(?,?,?,?,?)";

                     $cordovaSQLite.execute(db,questionsql,[value.id,value.question_status,value.question,value.question_order,value.last_modified]).then(function(result){
                        console.log(result.insertId);
                     },function(error){
                        console.log(error.message);
                     })
                });


                    $timeout(function(){

                    },2000).then(function(){

                        //q.resolve(true);
                    })


            },function(error){
                q.reject();
            });
            return q.promise;
        }

    }
});

$cordovaSQLite.execute的调用返回一个诺言.

The call to $cordovaSQLite.execute returns a promise.

在所有的诺言都解决之后,我想返回true.如何解决每个循环中已解决的所有诺言?

I want to return true after all the promises are resolved. How can I resolve all the promises that are resolved in each loop?

搜索此内容时,我找到了$q.all作为答案.然后,我阅读了一些有关此的教程,但不能在此处实现.

When I searched about this I found $q.all as an answer. Then I have read some tutorials about this but cannot implement here.

推荐答案

是,使用 Promise.then()返回一个承诺,因此,如果不删除.then()回调,则需要对其进行更新以返回参数(即result).

Yes use $q.all(). Push the promises into the array, but remove the .then() callbacks. Promise.then() returns a promise so if the .then() callbacks are not removed, they would need to be updated to return the arguments (i.e. result).

promises.push($cordovaSQLite.execute(db,sharingsql,[value.id,value.content_order,value.content,value.last_modified]))

然后通过调用.then(),可以在其中返回true:

Then use $q.all(promises) by calling .then() on it, where true can be returned:

$q.all(promises)
  .then(function(responses) {
    //all promises have been resolved
    return true;
  })

此小矮人中查看演示.

这篇关于解决angularjs中的多个promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆