UnityWebRequestExtensions

Extension method that makes UnityWebRequestAsyncOperation directly awaitable in async/await code by providing a custom GetAwaiter() implementation. Bridges Unity's callback-based web requests with the Task-based async pattern.

Definition

Namespace: Paragon Assembly: Paragon.dll

public static class UnityWebRequestExtensions

Quick Lookup

Goal
Method

Await a web request

await webRequest.SendWebRequest()

Methods

GetAwaiter

Returns a TaskAwaiter<UnityWebRequest.Result> for a UnityWebRequestAsyncOperation, enabling the await keyword on Unity web request operations.

public static TaskAwaiter<UnityWebRequest.Result> GetAwaiter(this UnityWebRequestAsyncOperation operation)
Parameter
Type
Description

operation

UnityWebRequestAsyncOperation

The async operation returned by UnityWebRequest.SendWebRequest()

Returns: A TaskAwaiter<UnityWebRequest.Result> that completes when the web request finishes.

Implementation:

  1. Creates a TaskCompletionSource<UnityWebRequest.Result>

  2. Subscribes to operation.completed to set the result when the operation finishes

  3. Also checks operation.isDone immediately — if the operation completed synchronously before the callback was registered, sets the result right away

  4. Returns the TaskAwaiter from the underlying Task

circle-info

The immediate isDone check prevents a race condition: if the request completes before the completed callback is registered, the TaskCompletionSource would never complete without this guard. TrySetResult is used (instead of SetResult) so that both paths can safely attempt to set the result without throwing.

Common Pitfalls

circle-exclamation
circle-exclamation

Examples

Basic awaitable request

With error handling

See Also

  • Yield — Paragon's async utilities for frame-based waiting

Last updated