[英]wait for angular2 observable to complete
我已經嘗試通過執行以下操作將現有訂閱轉換為Promise,但是我仍然在vm_payload中缺少“ dtype”:“ VNC”及其相關屬性。 我看不到我在做什么錯。
我嘗試遵循, angular2-在observable完成后導航 ,這應該可以完成這項工作。
customSubmit(value) {
const hdd = value.datastore+"/"+value.name.replace(/\s+/g, '-')+"-"+Math.random().toString(36).substring(7);
const payload = {}
const vm_payload = {}
payload["name"] = hdd
payload["type"] = "VOLUME";
payload["volsize"] = value.volsize * 1024 * 1000 * 1000;
payload["volblocksize"] = "512";
vm_payload["vm_type"]= "Bhyve";
vm_payload["memory"]= value.memory;
vm_payload["name"] = value.name;
vm_payload["vcpus"] = value.vcpus;
vm_payload["memory"] = value.memory;
vm_payload["bootloader"] = value.bootloader;
vm_payload["autoloader"] = value.autoloader;
vm_payload["devices"] = [
{"dtype": "NIC", "attributes": {"type": value.NIC_type, "mac": value.NIC_mac, "nic_attach":value.nic_attach}},
{"dtype": "DISK", "attributes": {"path": hdd, "type": "AHCI", "sectorsize": 0}},
{"dtype": "CDROM", "attributes": {"path": value.iso_path}},
]
if(value.enable_vnc){
this.create_vnc_device(vm_payload);
};
this.loader.open();
if( value.hdd_path ){
for (const device of vm_payload["devices"]){
if (device.dtype === "DISK"){
device.attributes.path = '/dev/zvol/'+ value.hdd_path.substring(5);
};
};
console.log("OLD DISK",vm_payload);
this.ws.call('vm.create', [vm_payload]).subscribe(vm_res => {
this.loader.close();
this.router.navigate(['/vm']);
},(error) => {
this.loader.close();
});
} else {
this.ws.call('pool.dataset.create', [payload]).subscribe(res => {
for (const device of vm_payload["devices"]){
if (device.dtype === "DISK"){
const orig_hdd = device.attributes.path;
device.attributes.path = '/dev/zvol/' + orig_hdd
};
};
console.log("NEW DISK",vm_payload);
this.ws.call('vm.create', [vm_payload]).subscribe(vm_res => {
this.loader.close();
this.router.navigate(['/vm']);
});
},(error) => {
this.loader.close();
});
}
}
create_vnc_device(vm_payload: any) {
this.ws.call('interfaces.ipv4_in_use').subscribe(res=>{
vm_payload["devices"].push(
{
"dtype": "VNC", "attributes": {
"wait": true,
"vnc_port": String(this.getRndInteger(5553,6553)),
"vnc_resolution": "1024x768",
"vnc_bind": res[0],
"vnc_password": "",
"vnc_web": true
}
}
);
console.log("VM PAYLOAD",vm_payload);
});
}
}
鄧諾(Dunno)您在這里做什么hm,實際上,如果您在承諾范圍內獲得承諾,則不應退還諾言。
create_vnc_device(vm_payload: any) {
this.ws.call('interfaces.ipv4_in_use').toPromise().then(res=>{
vm_payload["devices"].push(
{
"dtype": "VNC", "attributes": {
"wait": true,
"vnc_port": String(this.getRndInteger(5553,6553)),
"vnc_resolution": "1024x768",
"vnc_bind": res[0],
"vnc_password": "",
"vnc_web": true
}
}
);
});
}
它必須像那樣工作,並更新vm_payload
。
所以我最終按照angular2中的描述使用了async-await
await-在observable完成后進行導航 ,我在create_vnc_device
上缺少async-await
,並且我還需要在
async customSubmit(value) {
...................SNIP..............
if(value.enable_vnc){
await this.create_vnc_device(vm_payload);
...................SNIP..............
};
}
async create_vnc_device(vm_payload: any) {
await this.ws.call('interfaces.ipv4_in_use').toPromise().then( res=>{
vm_payload["devices"].push(
{
"dtype": "VNC", "attributes": {
"wait": true,
"vnc_port": String(this.getRndInteger(5553,6553)),
"vnc_resolution": "1024x768",
"vnc_bind": res[0],
"vnc_password": "",
"vnc_web": true
}
}
);
});
}
相關的git commit。
https://github.com/freenas/webui/pull/423/commits/1d756f43a7fd9efbd84d089d5a322c57b0cc4c8e
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.