Here is what I'm trying to do. I need to do some pre-processing on a JSON object. In order to do so, I need to loop through each element, do a promise for a new person if there is no id for that person, and then update that element in the list.
Example:
participant:[{
person:{
firstName: 'john'
lastName: 'Doe'
},
role: 'Plumber'
}, {
person: '12345a9j09kf23f3'
role: 'Window Washer'
}]
The first element doesn't have a person id, so I'm going to create a promise, create the person in the database and then update that element and replace 'John Doe' with a personId. The second element already has an id and doesn't need to go to the database to create a new person.
This code works as is. The problem is that I am trying to do a for loop and I need a synchrounous promise. I am in trouble. How can I use a loop and have a promise called or conditionally not handle a promise and the logic work synchronously?
Box.create(box)
.then(function(data) {
_.forEach(participants, function (participant) {
//This promise will be executed asynchronously, possible to make it synchronous with the for loop?
if (typeof participant.person != 'undefined' && typeof participant.person.firstName != 'undefined') {
Person.create(participant.person).then(function (data) {
return Participant.create({}).then(function(data){
newParticipants.push(data);
})
});
} else {
participants.push(participant);
}
});
return Q(undefined);
}).then(function(){
// more logic