Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set timeout #6

Open
Avatarchik opened this issue Sep 21, 2023 · 1 comment
Open

set timeout #6

Avatarchik opened this issue Sep 21, 2023 · 1 comment

Comments

@Avatarchik
Copy link

how can I set a timeout?

@selfsx
Copy link
Collaborator

selfsx commented Sep 22, 2023

Hi. At this time, the library does not have any decorator to access UnityWebRequest.timeout property. But you can simulate it for your request with something like this:

var request = new As<string>(new Get(new Text("https://google.com")));
var timeout = Task.Delay(TimeSpan.FromSeconds(4.0f));

await Task.WhenAny(AsTask(request), timeout);

Given that any request is awaitable you can write your AsTask method like this:

    private static async Task<T> AsTask<T>(IAwaitable<T> awaitable) {
      var tcs = new TaskCompletionSource<T>();

      try {
        var result = await awaitable;
        tcs.SetResult(result);
      } catch (Exception e) {
        tcs.SetException(e);
        return default;
      }

      return await tcs.Task;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants