等待 angular2 observable 完成

wait for angular2 observable to complete

我已尝试通过执行以下操作将现有订阅转换为承诺,但我仍然得到 vm_payload is devoid of "dtype": "VNC" 及其相关属性。我看不出我做错了什么。

我已经尝试遵循,,应该可以完成这项工作。

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);
    }); 
  }
}

不知道你在这里做什么嗯,实际上你不应该return承诺,如果你在功能范围内得到它。

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

所以我最终按照 中的描述使用了 async-await,我在 create_vnc_device 上丢失了 async-await,而且我需要在 [=16= 中使用它]

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 提交。

https://github.com/freenas/webui/pull/423/commits/1d756f43a7fd9efbd84d089d5a322c57b0cc4c8e