繁体   English   中英

等待angular2可观察到的完成

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM