Tenekon.Coroutines
0.1.0-preview.2
Prefix Reserved
See the version list below for details.
dotnet add package Tenekon.Coroutines --version 0.1.0-preview.2
NuGet\Install-Package Tenekon.Coroutines -Version 0.1.0-preview.2
<PackageReference Include="Tenekon.Coroutines" Version="0.1.0-preview.2" />
<PackageVersion Include="Tenekon.Coroutines" Version="0.1.0-preview.2" />
<PackageReference Include="Tenekon.Coroutines" />
paket add Tenekon.Coroutines --version 0.1.0-preview.2
#r "nuget: Tenekon.Coroutines, 0.1.0-preview.2"
#:package Tenekon.Coroutines@0.1.0-preview.2
#addin nuget:?package=Tenekon.Coroutines&version=0.1.0-preview.2&prerelease
#tool nuget:?package=Tenekon.Coroutines&version=0.1.0-preview.2&prerelease
Tenekon.Coroutines

Write C# async-await coroutines in a Redux-Saga fashion and let them behave like JavaScript generators
You found a bug, have ideas or just want to talk? Join the Tenekon Community, start a discussion or open an issue.
Tenekon.Coroutines key facts on a glance
- Async iterators: make coroutines iterable
- Each suspension point allows to clone the async iterator
- Fully AOT-compatible
- Highly extendible via new yielders and/or custom coroutine context
- Child coroutines inherit context of their parents: reduce explicit dependency injection
- Almost as fast as
ValueTask
Installation
Minimum requirements
- Target framework that at least supports NET Standard 2.1 or .NET 6.0
Package Manager
Install-Package Tenekon.Coroutines -Version <type version here>
.NET CLI
dotnet add package Tenekon.Coroutines --version <type version here>
Quick start guide
Simple Coroutine
using static Tenekon.Coroutines.Yielders;
await Coroutine.Start(asnyc () => {
await Call(Console.WriteLine, "Hello world");
});
// or
await Func<Coroutine>(asnyc () => {
await Call(Console.WriteLine, "Hello world");
});
// Outputs:
// Hello world
Simple AsyncIterator
using static Tenekon.Coroutines.Yielders;
using static Tenekon.Coroutines.Yielders.Arguments;
var iterator = AsyncIterator.Create(asnyc () => {
await Call(Console.WriteLine, "Hello world");
});
while (await iterator.MoveNextAsync()) {
if (iterator.CurrentKey == CallKey) {
Console.WriteLine(((CallArgument<string>)iterator.Current).Closure);
}
}
// Outputs:
// Hello world
Advanced AsyncIterator
using static Tenekon.Coroutines.Yielders;
using static Tenekon.Coroutines.Yielders.Arguments;
var iterator = AsyncIterator.Create(asnyc () => {
await Call(Console.WriteLine, "Hello world");
});
while (await iterator.MoveNextAsync()) {
if (iterator.CurrentKey == CallKey) {
Console.WriteLine(((CallArgument<string>)iterator.Current).Closure);
iterator.Current = new CallArgument<string>(Console.WriteLine, "Hello iterator")
}
}
// Outputs:
// Hello world
// Hello iterator
Advanced AsyncIterator
using static Tenekon.Coroutines.Iterator.Yielders;
using static Tenekon.Coroutines.Iterator.Yielders.Arguments;
var iterator = AsyncIterator.Create(asnyc () => {
Console.WriteLine(await Exchange("Hello world"));
});
while (await iterator.MoveNextAsync()) {
if (iterator.CurrentKey == ExchangeKey) {
Console.WriteLine(((CallArgument<string>)iterator.Current).Closure);
iterator.YieldReturn("Hello iterator");
}
}
// Outputs:
// Hello world
// Hello iterator
Quick references
Yielders are the equivalent to effects in Redux-Saga.
In the following are all available Yielders classes and their contained yielders listed.
We recommend to make use of using static, e.g. using static Tenekon.Coroutines.Yielders.
Tenekon.Coroutines.Yielders
Call
The Call yielder is the equivalent to the call effect in Redux-Saga.
Use it to instruct the coroutine to suspend and invoke provider with optional closure. The coroutine resumes as soon as provider completed.
| Yielder | Signature |
|---|---|
Call |
Coroutine Call(Func<Coroutine> provider) |
Call |
Coroutine Call<TClosure>(Func<TClosure, Coroutine> provider, TClosure closure) |
Call |
Coroutine<TResult> Call<TResult>(Func<Coroutine<TResult>> provider) |
Call |
Coroutine<TResult> Call<TClosure, TResult>(Func<TClosure, Coroutine<TResult>> provider, TClosure closure) |
Launch
The Launch yielder is the equivalent to the fork effect in Redux-Saga.
Use it to instruct the coroutine to suspend and invoke provider with optional closure. The coroutine resumes immediatelly, but in case coroutine returns before provider finished it will suspend as long as provider won't have completed.
| Yielder | Signature |
|---|---|
Launch |
Coroutine<CoroutineAwaitable> Launch(Func<Coroutine> provider) |
Launch |
Coroutine<CoroutineAwaitable> Launch<TClosure>(Func<TClosure, Coroutine> provider, TClosure closure) |
Launch |
Coroutine<CoroutineAwaitable<TResult>> Launch<TResult>(Func<Coroutine<TResult>> provider) |
Launch |
Coroutine<CoroutineAwaitable<TResult>> Launch<TClosure, TResult>(Func<TClosure, Coroutine<TResult>> provider, TClosure closure) |
Spawn
The Spawn yielder is the equivalent to the spawn effect in Redux-Saga.
Use it to instruct the coroutine to suspend and invoke provider with optional closure. The coroutine resumes immediatelly, but in case coroutine returns before provider finished it will just return and provider will just continue to operate independently.
| Yielder | Signature |
|---|---|
Spawn |
Coroutine<CoroutineAwaitable> Spawn(Func<Coroutine> provider) |
Spawn |
Coroutine<CoroutineAwaitable> Spawn<TClosure>(Func<TClosure, Coroutine> provider, TClosure closure) |
Spawn |
Coroutine<CoroutineAwaitable<TResult>> Spawn<TResult>(Func<Coroutine<TResult>> provider) |
Spawn |
Coroutine<CoroutineAwaitable<TResult>> Spawn<TClosure, TResult>(Func<TClosure, Coroutine<TResult>> provider, TClosure closure) |
Spawn
Use Throw to instruct the coroutine to suspend and let the coroutine throw exception. The coroutine resumes immediatelly and throws excpetion.
| Yielder | Signature |
|---|---|
Throw |
Coroutine Throw(Exception exception) |
WithContext
Use WithContext to instruct the coroutine to suspend and invoke provider with additiveContext and optional closure.
| Yielder | Signature |
|---|---|
WithContext |
Coroutine WithContext(CoroutineContext additiveContext, Func<Coroutine> provider) |
WithContext |
Coroutine WithContext<TClosure>(CoroutineContext additiveContext, Func<TClosure, Coroutine> provider, TClosure closure) |
WithContext |
Coroutine<TResult> WithContext<TResult>(CoroutineContext additiveContext, Func<Coroutine<TResult>> provider) |
WithContext |
Coroutine<TResult> WithContext<TClosure, TResult>(CoroutineContext additiveContext, Func<TClosure, Coroutine<TResult>> provider, TClosure closure) |
Yield
Use YieldReturn to instruct the coroutine to suspend and resume immediatelly. Allows the coroutine middleware to get the hands on value.
| Yielder | Signature |
|---|---|
YieldReturn |
Coroutine YieldReturn<T>(T value) |
Tenekon.Coroutines.Iterators.Yielders
Use Exchange to instruct the coroutine to suspend and resume immediatelly. Allows the coroutine middleware to get the hands on value and exchange it.
| Yielder | Signature |
|---|---|
Exchange |
Coroutine<T> Exchange<T>(T value) |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- IsExternalInit (>= 1.0.3)
- K4os.Hash.xxHash (>= 1.0.8)
- Nullable (>= 1.3.1)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
net6.0
- K4os.Hash.xxHash (>= 1.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0-preview.4 | 86 | 10/6/2024 |
| 0.1.0-preview.3 | 70 | 10/6/2024 |
| 0.1.0-preview.2 | 75 | 9/26/2024 |
| 0.1.0-preview.1 | 78 | 9/26/2024 |