AsyncObservables.Program.RunningAsyncCodeInWhere C# (CSharp) Method

RunningAsyncCodeInWhere() private static method

private static RunningAsyncCodeInWhere ( ) : void
return void
        private static void RunningAsyncCodeInWhere()
        {
            Demo.DisplayHeader("Running async code int the pipeline - order is not determenistic");

            var svc = new PrimeCheckService();

            //
            // this wont Compile
            //
            //var subscription = Observable.Range(1, 10)
            //    .Where(async x => await svc.IsPrimeAsync(x))
            //    .SubscribeConsole("AsyncWhere");

            //
            // This compiles and runs - query syntax
            //
            IObservable<int> primes =
                from number in Observable.Range(1, 10)
                from isPrime in svc.IsPrimeAsync(number)
                where isPrime
                select number;

            //
            // The same, but in methods chain
            //
            primes =
                Observable.Range(1, 10)
                    .SelectMany((number) => svc.IsPrimeAsync(number),
                                 (number, isPrime) => new { number, isPrime })
                    .Where(x => x.isPrime)
                    .Select(x => x.number);

            var exampleResetEvent = new AutoResetEvent(false);
            primes
                .DoLast(() => exampleResetEvent.Set(), delay: TimeSpan.FromSeconds(1))
                .SubscribeConsole("primes");

            exampleResetEvent.WaitOne();
        }