问题描述
限时送ChatGPT账号..这是我的工厂,它从 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.
推荐答案
是使用 $q.all().将承诺推送到数组中,但删除 .then()
回调.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]))
然后通过调用使用 $q.all(promises) .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;
})
在 this plunker 中查看演示.
这篇关于解决angularjs中的多个承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论