Created
March 11, 2019 10:27
-
-
Save mrtank/25a7a4674d17fe213e3c24c247934248 to your computer and use it in GitHub Desktop.
Handling async in Rx.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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