0

I want to know if the code below executes the next statement while waiting for the async api call? If so, then the value would be null and might cause a null exception? Am I doing this correctly?

var response = await pl_httpClient.GetAsync("api/GetInfo?CardNo=" + CardNo);

if (!response.IsSuccessStatusCode) 
{ 
return response.StatusCode); 
}

InfoModel infoModel = await response.Content.ReadAsAsync<InfoModel>();

if(infoModel == null){ return "Card number is invalid"; }

if (infoModel.ExpiryDate < DateTime.Now.Date) { return "Expired Card Number"; }

if (!infoModel.MemberStatus.Equals("1")) { return "Inactive Card Number"; }
Kate Lastimosa
  • 169
  • 2
  • 15

1 Answers1

1

The way I like to think about it is that await pauses the method but not the thread. So for code like this:

var response = await pl_httpClient.GetAsync("api/GetInfo?CardNo=" + CardNo);
if (!response.IsSuccessStatusCode) 

The entire method is paused at the await statement until the download is complete. Then the method resumes executing, sets the response variable, and then proceeds with checking response.IsSuccessStatusCode.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810