Is there a way to wait the response of my Observer
before i return the value?
data(): MatTableDataSource<ITableData> {
var datasource: MatTableDataSource<ITableData> = new MatTableDataSource()
var ob: Observer<any> = {
next: (result: any) => {
const l = this.processData(result)
datasource = new MatTableDataSource(l)
},
error: (err: Error) => {
console.log(err)
},
complete: () => {
}
}
this.getDataFromJson().subscribe(ob);
console.log(datasource)
return datasource;
}
Right it doesn't return the value attributed in the observer but the new MatTableDataSource()
from when i declare it. Because the return
executes before my Observer
.
Expected Result: datasource.data = Array(15)
Result got: datasource.data = Array(0)
CodePudding user response:
Another way to do this is to declare your data source as a property on the component (or any other class that holds this logic) and only instantiate it once:
public datasource = new MatTableDataSource<ITableData>();
public loadData(): void {
this.getDataFromJson().subscribe((result: any) => {
this.datasource.data = this.processData(result);
});
}
You can now call loadData
whenever you need to refresh the data and your datasource will reflect the new changes.