/// <summary> /// Returns the synchronous task that will complete after an internal <see cref="ICoroutine"/> is completed. /// </summary> /// <param name="result">Container for storing the result.</param> /// <param name="coroutine">Implementation of the coroutine.</param> /// <exception cref="ArgumentNullException"> /// The <paramref name="coroutine"/> parameter is null. /// </exception> /// <example> /// <code> /// static IEnumerator<IRoutineAction> DoSomething() /// { /// var coroutine = new Coroutine(...); /// /// // Wait for the coroutine to complete. /// // At this point, execution pass to another routine. /// yield return Routine.Await(out var result, coroutine); /// /// // Print what `coroutine.GetResult()` returned. /// Console.WriteLine($"{result.Value}"); /// } /// </code> /// </example> public static ICoroutine Await(out AwaitResult <object?> result, ICoroutine coroutine) { if (coroutine == null) { throw new ArgumentNullException(nameof(coroutine)); } result = new AwaitResult <object?>(); return(new SyncWaitTCoroutine(result, coroutine)); }
/// <summary> /// Returns the asynchronous task that will complete after an internal <see cref="Task{TResult}"/> is completed. /// </summary> /// <param name="result">Container for storing the result.</param> /// <param name="taskFactory">Task factory function.</param> /// <exception cref="ArgumentNullException"> /// The <paramref name="taskFactory"/> parameter is null. /// </exception> /// <example> /// <code> /// static IEnumerator<IRoutineAction> DoSomething() /// { /// // Wait for the task to complete. /// // At this point, execution pass to another routine. /// yield return Routine.Await(out var result, async () => /// { /// using var client = new HttpClient(); /// /// return await client.GetStringAsync("https://www.google.com/"); /// }); /// /// Console.WriteLine($"Length: {result.Value.Length}"); // Length: 49950 /// } /// </code> /// </example> public static ICoroutine Await <TValue>(out AwaitResult <TValue> result, Func <Task <TValue> > taskFactory) { if (taskFactory == null) { throw new ArgumentNullException(nameof(taskFactory)); } result = new AwaitResult <TValue>(); return(new AsyncWaitTCoroutine <TValue>(result, async res => res.Value = await taskFactory())); }