The problem here is that when you do fileService.uploadFile.subscribe(), this subscription is carried out out asynchronously. The subscription waits till it gets a result, meanwhile the execution of the lines after the subscription is carried out immedialely. So, the return ids is executed right after the subscription. At this point the result has no value and nothing is pushed into ids. So, it just returns empty array.
You can declare ids globally. So you can push into them when result is ready and can use the ids or you can make the this function return an observable and use subjects to provide the values. It is shown in the code below.
private idsSubject = new Subject<number[]>();
async uploadFiles(token: string):Observable<number[]> { //made return type observable
this.images.forEach((image: ImageData) => {
this.fileService.uploadFile(image, token).subscribe((result: File) => {
this.idsSubject.next(result.data.id);
})
});
return this.idsSubject.asObservable();
}
When you want the ids subscribe tho this function. Like uploadFiles('my string').subscribe((id)=>this.ids.push(id))
Try this, hope it helps. Tell me otherwise