I am trying to consume an API I developed in NODE.js, I am doing this from a c# windows forms desktop application.
The API is written ASYNC on the sever side but can it be sync inside my client? Let me explain what I mean.
This is a code sample of what I am doing:
public static DateTime GetDateTime()
{
try
{
string result = Task.Run(() =>
client.GetStringAsync(client.BaseAddress)).Result;
Date currentTime = JsonConvert.DeserializeObject<Date>(result);
return currentTime.Value;
}
catch (Exception ex)
{
throw ex;
}
}
Later in the program I call this function:
DateTime currentDate = GymAPI.GetDateTime();
According to what I researched this runs synchronously... and this is what I need because after the function call I use the datetime to calculate and display the age of a list of persons.
As I understand, if i use ASYNC/AWAIT, the code that calculates the age of the persons will execute right away while most likely I will not have yet the value of the current date. Am I correct in this assumption?
Do I need to run anything ASYNC in my app other than when I am sending an email (takes like 5 seconds) and I want the sendmail task to run in the background while the app remains responsive to the user?
Finally, and more important, the above code seems to work but... Is the way I did it the best practice to make the call run synchronously? No deadlocks? Thanks for your patience reading this but I found a lot of posts and honestly I couldn't find all the answers.
If there are too many questions please only answer the last one! :)