【问题标题】:typescript async-await causing variable to be undefined打字稿异步等待导致变量未定义
【发布时间】:2018-03-21 11:32:41
【问题描述】:

我有一段时间面临这个问题,其中vm_res 未定义 async update_vm_raw_device 函数,函数update_vm_raw_device 运行,但值更新为/mnt/tank/ds/test_file_test_VM_undefined 请注意最后未定义。

async customSubmit(value) {
  const path = value.raw_file_directory+ '/' + value.raw_filename+ '_'+ value.name;
    const payload = {}
    const vm_payload = {}
    ......SNIP..........
    this.ws.call('vm.get_sharefs').subscribe((get_sharefs)=>{
      if(!get_sharefs){
        this.ws.call('vm.activate_sharefs').subscribe((sharefs)=>{
          this.ws.call('vm.create', [vm_payload]).toPromise().then(vm_res => {
            // here the assignment happens, but  this.vm_res is undefined in 
            // async update_vm_raw_device function.
            this.vm_res = vm_res;
          });
        }
      )
      }
      else {
        this.ws.call('vm.create', [vm_payload]).toPromise().then(vm_res => {
          this.vm_res = vm_res;
        });
      }
    },)
    await this.update_vm_raw_device(vm_payload, this.vm_res);
  }

  async update_vm_raw_device(vm_payload: any, vm_res: number) {
  vm_payload.path = vm_payload.path+"_"+this.vm_res
   await this.ws.call('datastore.update'[vm_payload])]).toPromise().then(
       res=>{});}

【问题讨论】:

    标签: angular typescript async-await


    【解决方案1】:

    async/await与此无关。

    据我所知,this.vm_res 只会在 subscribe 回调中设置,这在您调用 this.update_vm_raw_device() 之前不会发生。

    您是否打算在订阅处理程序中调用this.update_vm_raw_device()?也许在设置该值之后?

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2018-05-03
      • 2016-10-20
      • 1970-01-01
      相关资源
      最近更新 更多