开发者

Chaining continuations together using .NET Reactive

开发者 https://www.devze.com 2023-02-05 19:31 出处:网络
Newbie Rx question. I want to write a method like the following: public IObsevable<Unit> Save(object obj)

Newbie Rx question. I want to write a method like the following:

public IObsevable<Unit> Save(object obj)
{
   var saveFunc = Observable.FromAsyncPattern(...);
   saveFunc(obj).Subscribe(result =>
      {
         Process(result);
         return Observable.Return(new Unit());
      });
}

The basic idea is: Save the given object, process the results in my "inner" continuation, then allow the caller's "outer" continuation to execute. In other words, I want to chain two continuations together so that the second one does not execute until the first one finishes.

Unfortunately, the code above does not compile because the inner continuation has to return void rather than an IObservable. Plus, of course, returning an observable Unit out of a lambda is not the same as returning it from the containing function, which开发者_开发问答 is what I really need to do. How can I rewrite this code so that it returns the observable Unit correctly? Thanks.


Simplest solution is to use SelectMany

public IObsevable<Unit> Save(object obj)
{
   var saveFunc = Observable.FromAsyncPattern(...);
   return saveFunc(obj).SelectMany(result =>
   {
      Process(result);
      return Observable.Return(new Unit());
   });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消