I'm trying to move away from unity's coroutines and callbacks in favor of async/await.
I have the following code using coroutines:
public IEnumerator GetProfileJson(string address, GetProfileSuccess onSuccess, GetProfileFailed onFail)
{
var queryRequest = new QueryUnityRequest<GetDataFunction, GetDataOutputDTO>(RpcUrl, null);
yield return queryRequest.Query(new GetDataFunction(), address);
if(queryRequest.Result == null)
{
onFail?.Invoke(new NullResponseException());
yield break;
}
byte[] resultBytes = queryRequest.Result.Values[0];
string url = Encoding.UTF8.GetString(resultBytes);
using WebClient web = new WebClient();
string json = web.DownloadString(url);
onSuccess?.Invoke(json);
}
I would like to convert it to something like this:
public async Task<string> GetProfileJson(string address)
{
var queryRequest = new QueryUnityRequest<GetDataFunction, GetDataOutputDTO>(RpcUrl, null);
await Task.Run(() => // Somehow await this coroutine's completion
{
queryRequest.Query(new GetDataFunction(), address);
});
if(queryRequest.Result == null)
{
throw new NullResponseException();
yield break;
}
byte[] resultBytes = queryRequest.Result.Values[0];
string url = Encoding.UTF8.GetString(resultBytes);
using WebClient web = new WebClient();
string json = await web.DownloadStringTaskAsync(url);
return json;
}
But I just can't figure out how to run and await this line
queryRequest.Query(new GetDataFunction(), address);
The Query method is from Neuthereum's API and looks like this
public IEnumerator Query(string contractAddress, BlockParameter blockParameter = null)
{
yield return Query(new TFunctionMessage(), contractAddress, blockParameter);
}