[英]Promises using nodejs and sequelize
有沒有一種方法可以避免在下面的代碼中使用'q'嵌套嵌套,有什么好的方法可以與promise一起使用?
global.models.test.destroy({
where: {
id: req.params.id
}
}).then(function() {
global.models.test1.findAll({
attributes:[['id','testId']],
include:[{
model: global.models.test2,
where: {
masterId: req.params.id
},
required: true
}]
}).then(function(app){
var arrIds =[];
for(var result in app){
var collection = app[result].dataValues;
arrIds.push(collection.id);
}
global.models.test1.destroy({
where: {
id: arrIds
}
}).then(function() {
global.models.destroy({
// nested loops again and so on
}):
})));
我正在尋找避免嵌套循環來清理代碼的方法。 歡迎所有幫助
是的, then
起就兌現您的諾言。
global.models.test.destroy({
where: {
id: req.params.id
}
}).then(function() {
return global.models.test1.findAll({
attributes:[['id','testId']],
include:[{
model: global.models.test2,
where: {
masterId: req.params.id
},
required: true
}]
})
}).then(function(app) {
var arrIds = [];
for(var result in app) {
var collection = app[result].dataValues;
arrIds.push(collection.id);
}
return global.models.test1.destroy({
where: {
id: arrIds
}
});
}).then(function() {
return global.models.destroy({
//and so on
});
});
我還要避免污染global
變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.