I am trying to get my head around tuple for the first time using it in an async function. When testing I am not able to catch the response. I have been google the problem but not been able t solve it.
I want to run an async function that return three strings. I am not able to catch the response. Have tried referencing item or the name.
Here is the code:
var _moonAndSunResult = CleanSunAndMoonJson(_moonJson);
print(_moonAndSunResult);
Tried referencing the _moonAndSunResult in different ways.
// async Task<Tuple<string, string, string, string>> CleanSunAndMoonJson(string _json)
async Task<(string, string, string, string)> CleanSunAndMoonJson(string _json)
{
print("sunRise: " GetCorrectTimeFormat("sunRise", _json));
print("sunSet: " GetCorrectTimeFormat("sunSet", _json));
print("moonRise: " GetCorrectTimeFormat("moonRise", _json));
print("moonSet: " GetCorrectTimeFormat("moonSet", _json));
Utility.sunrise = GetCorrectTimeFormat("sunRise", _json);
Utility.sunset = GetCorrectTimeFormat("sunSet", _json);
Utility.moonrise = GetCorrectTimeFormat("moonRise", _json);
Utility.moonset = GetCorrectTimeFormat("moonSet", _json);
await Task.Yield();
//return (firstValue, secondValue, thirdValue);
//return new Tuple<string, string, string, string>(Utility.sunrise, Utility.sunset, Utility.moonrise, Utility.moonset);
return (Utility.sunrise, Utility.sunset, Utility.moonrise, Utility.moonset);
//return Tuple.Create(Utility.sunrise, Utility.sunset, Utility.moonrise, Utility.moonset);
}
I get this from the _moonAndSunResult print above
System.Threading.Tasks.Task`1[System.ValueTuple`4[System.String,System.String,System.String,System.String]]
CodePudding user response:
What @YoungDeiza said (await the task), but really you don't need to use tasks here:
(string Sunrise, string Sunset, string Moonrise, string Moonset) CleanSunAndMoonJson(string _json)
{
...
var sunrise = GetCorrectTimeFormat("sunRise", _json);
var sunset = GetCorrectTimeFormat("sunSet", _json);
var moonrise = GetCorrectTimeFormat("moonRise", _json);
var moonset = GetCorrectTimeFormat("moonSet", _json);
return (sunrise, sunset, moonrise, moonset);
}
then
var _moonAndSunResult = CleanSunAndMoonJson(_moonJson);
CodePudding user response:
You need to await the asynchronous method:
public static async Task Main()
{
var _moonAndSunResult = await CleanSunAndMoonJson(_moonJson);
print(_moonAndSunResult);
}
If you do not await it, you call print on a task not the result of the task.