Im getting data of class People
array with each person having attendance list as Observable<any[]>
.
// Class person
class Person {
id: number;
name: string;
attendance: Observable<any[]>; // An Observable array
}
// People array (I only use array for simplicity sake)
// NOTE: Every person has a list of attendance
people = Person[] = this.someService.getPeople();
Now I'm validating if some of the people may had 0 length of attendance assuming that attendance: Observable<any[]>
is always an empty array []
if nothing is found, return false otherwise true to validateAttendance()
.
validateAttendance(): boolean {
// Loop to each person and get attendance list.
people.forEach(p => {
// Get the list using a callback function.
const func = (callback: (data) => void) => {
// Get attendance using subscribe() method
p.attendance.subscribe(list => {
callback(list);
});
}
// An attempt to get the list from a callback function
// Failed because return is undefined.
const result = func(res => {
console.log(res); // Got list of attendance of a given person.
return res;
});
if (result.length <= 0) {
alert(`${p.name} has no attendance, must be a lazy one please check.`);
return false;
}
});
return true;
}
In order to do that i attempted to create a callback function inside the validateAttendance()
method and return the attendance list from it. HOWEVER the result variable is undefined!
It is possible to return a value from a callback function this way?