Skip to content

Instantly share code, notes, and snippets.

@mrtank
Created March 11, 2019 10:27
Show Gist options
  • Save mrtank/25a7a4674d17fe213e3c24c247934248 to your computer and use it in GitHub Desktop.
Save mrtank/25a7a4674d17fe213e3c24c247934248 to your computer and use it in GitHub Desktop.
Handling async in Rx.
namespace PrimeFactors
{
using System;
using System.Net.Http;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
class ReqresReader
{
private readonly HttpClient _client = new HttpClient();
private async Task<IObservable<User>> IdWithPossibleWait(int i, CancellationToken tok)
{
Task<UsersListResponse> theTask = (await _client.GetAsync($"https://reqres.in/api/users?page={i}", tok)).Content.ReadAsAsync<UsersListResponse>(tok);
return Observable.FromAsync(() => theTask).SelectMany(userListResp => userListResp.Data);
}
public void Do()
{
CancellationTokenSource source = new CancellationTokenSource();
var observable = Observable.Range(1, 2).SelectMany(async (i, tok) => await IdWithPossibleWait(i, tok)).Merge();
observable.Subscribe(Console.WriteLine, source.Token);
Console.ReadKey();
source.Cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment