Closed AleksMeshkov closed 10 years ago
You'll need to use an asynchronous for-loop. There are other libraries that offer utilities for this, such as async.js. I also wrote up a blog post a while back showing how to asynchronously do for loops in JavaScript.
@zefhemel thank you! I'll dive into this.
Well here is my solution. I applied example of a managed control flow written here http://book.mixu.net/node/ch7.html
// fetch all
var get = function (callback) {
Specialist.all().list(function (items) {
var specialists = [];
// that's an async function
function populateWithTags(item, callback) {
item._tags = [];
item.tags.list(null, function (tags) {
tags.forEach(function (tag) {
item._tags.push({
tag : tag.tag
});
});
callback(item);
});
}
function series(item) {
if (item) {
populateWithTags(item, function(itemWithTags){
specialists.push(itemWithTags);
return series(items.shift());
});
} else {
return callback(specialists);
}
}
series(items.shift());
});
}
};
Hi! I have a Specialist model that has many Tags (tags are describing specialist skills). I want to fetch all specialists and pre-populate them with appropriate tags while fetching them from db. The issue I ran into is about executing callback when data retrieve is finished (including tags relationships). Could you please look through my code below? Is there any possibility to use Deferred with promises in this case?