I am trying to populate an array zvols which contains name of the path of a list of zvols. The problem is I am getting an empty array after successful get call,
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable, Subject, Subscription } from 'rxjs/Rx';
import { EntityUtils } from '../pages/common/entity/utils'
import { RestService, WebSocketService } from '../services/';
@Injectable()
export class VmService {
protected volume_resource_name: string = 'storage/volume'
constructor(protected rest: RestService){
};
getStorageVolumes() {
let zvols: Array<any> = [];
this.rest.get(this.volume_resource_name , {}).subscribe((res) => {
let data = new EntityUtils().flattenData(res.data);
for (let zvol_obj of data){
if (zvol_obj.type === 'zvol') {
zvols.push(zvol_obj.path);
}
}
console.log("BEFORE:" + zvols);
});
console.log("AFTER:" + zvols);
return zvols;
}
}
I can see that console prints
AFTER:
main.bundle.js:744 Unknown message: Object {msg: "ready", subs: Array(1)}
vm.service.ts:26 BEFORE:tank-a/ds-a/zvol-a,tank-b/ds-b/zvol-b
I am confused about this behavior. :(
console.log()is actually called afterreturn zvols, in the callback function. You can't return the result of async operation like that.get()fromgetStorageVolumes(), then the code that calls it should do asubscribe()and use the result in the callback it provides tosubscribe().