Skip to content

Instantly share code, notes, and snippets.

@shiena
Last active October 10, 2020 11:31
Show Gist options
  • Save shiena/5abbd0930d17159d1d27042f3bffb2c6 to your computer and use it in GitHub Desktop.
Save shiena/5abbd0930d17159d1d27042f3bffb2c6 to your computer and use it in GitHub Desktop.
public void Foo(Action<int> callback)
{
}
#region UniTask wrapper
private CancellationTokenSource _source;
public async void FooAsync()
{
using(_source = new CancellationTokenSource())
{
try
{
var source = new UniTaskCompletionSource<int>();
using (_source.Token.Register(() => source.TrySetCanceled()))
{
Foo(x => source.TrySetResult(x));
var result = await source.Task;
Debug.Log(result);
}
}
catch (OperationCanceledException e)
{
Debug.LogException(e);
}
}
}
public void Cancel()
{
_source.Cancel();
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment