I'm having a weird situation where I want to break a for loop after I have received the result of an Rx Promise and done some checks. What I have is the following:
function getDrift(groups) {
var drift = {};
groups.forEach(function(group) {
if(group.type === 'something') {
for(var i = 0; i < group.entries.length; i++) {
fetchEntry(group.entries[i].id)
.then(function(entry) {
if(entry.type === 'someType'){
drift[entry._id] = getCoordinates(entry);
// break;
}
});
}
}
});
return drift;
}
where fetchEntry
is returning a Promise of a mongodb document based on an id. If the if
check is satisfied, I want to break the loop on the current group.entries
and continue on to the next group.
Is that possible?
Thanks
EDIT: As requested, the groups object looks like this:
[
{
type: 'writing',
entries: [{id: "someId", name: "someName"}, {id: "someId2", name: "someName2"}]
},
{
type: 'reading',
entries: [{id: "someId3", name: "someName3"}, {id: "someId4", name: "someName4"}]
}
]
SOLUTION: I ended up using @MikeC 's suggestion with recursion and a callback to return the needed value. Thank you all!