Update README.md

Fleshing out ToAsyncLazy example.
pull/101/head
Liam Mitchell 2020-06-18 19:55:19 +12:00 committed by GitHub
parent d6a0563319
commit dab4625cf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 1 deletions

View File

@ -208,7 +208,37 @@ await task;
await task; // NG, throws Exception await task; // NG, throws Exception
``` ```
Store to the class field, you can use `UniTask.Lazy` that gurantee call multipletimes. `.Prevent()` allows for multiple calls (internally cached results). This is useful when multiple calls in a function scope. To allow awaiting multiple times you can make use of AsyncLazy.
```csharp
async UniTask<string> TestAsync(string text)
{
Debug.Log("TestAsync called: " + text);
// Pretend there is a slow load.
await UniTask.Delay(TimeSpan.FromSeconds(5));
return text;
}
public async void TestFunc() {
var task = TestAsync("Testing");
var asyncLazyTestAsync = task.ToAsyncLazy();
Debug.Log("Before all");
Debug.Log("Result 1: " + await asyncLazyTestAsync);
Debug.Log("Result 2: " + await asyncLazyTestAsync);
Debug.Log("After all");
}
```
If you want to store the task/result as a proprety you can declare it like this.
```csharp
private AsyncLazy<string> _lazyTask;
```
`UniTask<T>.ToAsyncLazy` calls `.Prevent()` which allows for multiple calls (the results are internally cached).
This is useful when multiple calls in a function scope.
Cancellation and Exception handling Cancellation and Exception handling
--- ---