I have a function getData()
that calls an API, processes the data, transforms, and saves it to a BehaviorSubject. Then returns the processed data as an observable using of().
getData()
getData(fId: string | null = null) {
// get the data to tempItems with an API Call
this.getDataFromAPI().subscribe(data => {
this.tempItems = [data]
})
let items = cloneDeep(this.tempItems);
// Separate the items by folders and files
const a: Item[] = items.filter(item => item.type === 'a');
const b: Item[] = items.filter(item => item.type !== 'b');
const path: Item[] = [];
//do some work
// Create data
let data = {
folders: a,
files: b,
path
}
// Add this data to the BehaviorSubject
this._items.next(data);
return of(data);
}
getDataFromAPI()
getDataFromAPI() {
return this._httpClient.get(URL);
}
In the getData()
function the variable this.tempItems
is undefined in run time. Because of the HTTP call, the rest of the script is not waiting for the HTTP request to be finished.
How can I properly use Async Await here? and still, manage to return the of(data)
The function which call the getData() function expects an observable in return