I have a scenario in Angular where I am using a chain of promises and I want to ensure that a value is returned at the end of the chain:
this.calculateeventsattended = function(username) {
var Attendees = Parse.Object.extend("Attendees");
var User = Parse.Object.extend("User");
var count_pres = 0
query1 = new Parse.Query(Attendees);
query1.equalTo("user_id",username);
query1.equalTo("presence",true)
var promise = query1.count(function(count){
count_pres = count
}).then(function(){
query2 = new Parse.Query(User);
query2.equalTo("username",username);
query2.first().then(function(object){
alert("parse" + count_pres)
object.set("events_attended",count_pres)
object.save()
})
})
$q.all(promise)
return count_pres
}
However, I am facing issues where the 'promise' chain is not resolved before the return statement is executed. This means that count_pres is being returned before $q.all(promise) has finished execution. Any suggestions on how to address this?