I'm entirely new to modern JavaScript. I'm trying to populate an array with data I get from a promise from a helperService.
export class PeopleTableComponent implements OnInit {
people: Array < Person > = [];
constructor(public helperService: HelperService) {}
ngOnInit(): void {
this.helperService.getPeople().then(res => {
res["people"].forEach(function(person) {
let person = new Person();
person.name = person.name;
this.people.push(person);
});
});
}
}
class Person {
name: string;
}
However this.people.push(person);
doesn't work since this
is undefined
(as I verified in debugging).
In other frameworks, I used to solve this with .bind(this)
. Maybe this would work here but is it the correct way? What is the recommended way to pass the context inside the promise callback?