我遇到这个问题有一段时间了,在异步update_vm_raw_device函数中vm_res是未定义的,函数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=>{});}发布于 2018-03-21 11:32:41
async/await与此无关。
据我所知,this.vm_res只会在subscribe回调中被设置,这在你调用this.update_vm_raw_device()之前是不会发生的。
您是否打算将对this.update_vm_raw_device()的调用放在订阅处理程序中?也许在设置该值之后?
https://stackoverflow.com/questions/49397587
复制相似问题