C#C
C#3y ago
wedmaniac

❔ Async function seems to continue running after returning.

Hi there I'm doing a post request to a REST API. I can see that the callback is running in the console when its printing the Debug.Log. So I would assume that the function would return a User that isn't null but it doesn't. I'm a bit new to asynchronous code and would love to know if I got all of this wrong and if you know a solution to my problem.
public static async Task<User> SignInAnonymous() {
  RestClient.Post<ExchangeRefreshTokenResponse>(currentRequest).Then(response => {
    Debug.Log(("IDTOKEN: " + response.id_token, "REFRESHTOKEN: " + response.refresh_token, "USERID: " + response.user_id));
    IdToken = response.id_token;
    LocalId = response.user_id;
    RefreshToken = response.refresh_token;
    RefreshTokenChanged.Invoke(RefreshToken);
    return new User("Anonymous", response.user_id, response.id_token);

  }).Catch(error => {
      Debug.Log(error.Message);
  });
  await Task.Delay(REQUEST_TIMEOUT);
  return null;
}
Was this page helpful?