简体   繁体   English

为什么 async/await 在服务器调用时中断?

[英]Why does async/await break on server call?

I am trying to implement async/await with server call.我正在尝试通过服务器调用实现异步/等待。 However, the async function doesn't wait for the await ed function to get resolved.但是, async function 不会等待await ed function 得到解析。 Below is the code i have written.下面是我写的代码。

index.ts索引.ts

async verifyOtp(){
    this.invalidOtpLength=false;
    this.invalidOtp=false;
    if(this.otp.length < 6){
      this.invalidOtpLength=true;
    }else if(this.otp.length==6 && this.otp !=this.otpFromServer){
      this.invalidOtp=true;
      this.invalidOtpLength=false;

      //decrease the no of attempts by 1
      if(this.otpAttemptsRemaining>1){this.otpAttemptsRemaining-=1}
        else{document.getElementById('otpMaxAttemptsPopupButton').click()}
    }else{
      console.log("Before promise")
      await this.getDetailsForPanAndAadhaar();
      console.log("After promise")
      this.wizard.navigation.goToNextStep();
      console.log("Aadhar values: ", this.aadharValues);
      console.log("PAN values: ", this.panValues);
    }
  }


getDetailsForPanAndAadhaar(){
    return new Promise((resolve) =>{
      let obj={};
      obj['serviceName']='ePanDetlService'
      obj['aadhaarNum']=this.aadharNumber.value.replace(/\s/g, '');
      obj['uidaiDetailsFlag']='Y';
      obj['otp']=this.otp;
      this.http.put('http://localhost:8080/servicesapi/updateEntity', obj).subscribe((data) =>{
          console.log("Initial data ", data);
          let aadharRegex=new RegExp(/^aadhaar./);
          let panRegex=new RegExp(/^pan./);
          delete data['header'];
          delete data['messages'];
          console.log("New data: ", data);
          for(let key in data){
            if(key.match(aadharRegex)){
              this.aadharValues[key]=data[key];
            }else if(key.match(panRegex)){
              this.panValues[key]=data[key];
            }
          }
        })
        resolve(null);
    })
  }

In the above code when the getDetailsForPanAndAadhaar() function is implemented as it is, in the console, the statements Before Promise and After Promise are displayed before the promise gets resolved (the data is consoled after the two statements).在上面的代码中,当getDetailsForPanAndAadhaar() function 原样实现时,在控制台中,在 promise 得到解析之前,会显示语句Before PromiseAfter Promisedata在两条语句之后被控制台)。

However, on changing the implementation of getDetailsForPanAndAadhaar() as below, the code works fine.但是,在如下更改getDetailsForPanAndAadhaar()的实现时,代码工作正常。

changed getDetailsForPanAndAadhaar()更改了 getDetailsForPanAndAadhaar()

getDetailsForPanAndAadhaar(){
  return new Promise((resolve) =>{
    setTimeout(() =>{
      console.log("Inside promise")
      resolve("High Time....");
    }, 5000);
})

I also tried changing the resolve statement from resolve(null) to resolve(this.aadharValues) but it still doesn't work.我还尝试将 resolve 语句从resolve(null)更改为resolve(this.aadharValues)但它仍然不起作用。 What am i doing wrong here?我在这里做错了什么?

You are calling resolve(null);您正在调用resolve(null); without waiting for this.http.put(...) to finish.无需等待this.http.put(...)完成。 It might all work if you resolve the promise after the for loop finishes.如果您在 for 循环结束后解析 promise,则可能一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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