我想返回一个对象数组。我的代码如下所示:
var apps = [];
this.get('groups').then(function(groups)
{
groups.forEach(function(group)
{
self.store.find('module', group.get('module').get('id')).then(function(module)
{
self.store.find('app', module.get('app').get('id')).then(function(app)
{
if(!apps.contains(app))
apps.pushObject(app);
});
});
});
});
我想在 foreach 循环完成后返回所有应用程序,但我不知道。
最佳答案
您必须始终返回一个继续构建链条的 promise 。
var self = this;
return this.get('groups')
// Return an app for each group
.then(function (groups) {
return Ember.RSVP.Promise.all(groups.map(function (group) {
return self.store.find('module', group.get('module.id'))
.then(function (module) {
return self.store.find('app', module.get('app.id'));
});
}));
})
// Filter out duplicates
.then(function (apps) {
return apps.uniq();
});
关于ember.js - EmberJS 实现多个嵌套的 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24104724/