Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can convert methods of service so they return IObservable instead of taking callback as a parameter. In this case sequential workflow can be implemented using <strong>SelectMany</strong>, something like this...</p> <pre><code> WidgetService.Query() .SelectMany( widgets =&gt; { Widgets.Clear(); foreach (var w in widgets) { Widgets.Add(w); } return WidgetService.Load(); } ) .Do( defaultWidget =&gt; { if (defaultWidget != null) Default = defaultWidget; } ) .Subscribe( _ =&gt; { }, e =&gt; { throw e; } ); </code></pre> <p>However IMO F# asyncs will look much more clear (in sample I assume that methods of service returns Async> and Async respectively). Note that sample doesn't take in account what thread is modifying data fields, in real-world code you should pay attention to this:</p> <pre><code> let load = async { let! widgets = WidgetService.Query() Widgets.Clear() for w in widgets do Widgets.Add(w) let! defaultWidget = WidgetService.Load() if defaultWidget &lt;&gt; null then Default &lt;- defaultWidget return () } Async.StartWithContinuations( load, ignore, // success continuation - ignore result raise, // error continuation - reraise exception ignore // cancellation continuation - ignore ) </code></pre> <p><strong>EDITED</strong></p> <p>In fact it is possible to use technique with iterators you mentioned in your question:</p> <pre><code> private IEnumerable&lt;IObservable&lt;object&gt;&gt; Intialize() { var widgetsList = WidgetService.Query().Start(); yield return widgetsList; Widgets.Clear(); foreach (var w in widgetsList[0]) { Widgets.Add(w); } var defaultWidgetList = WidgetService.Load().Start(); yield return defaultWidgetList; if (defaultWidgetList[0] != null) Default = defaultWidgetList[0]; } Observable .Iterate(Intialize) .Subscribe( _ =&gt; { }, ex =&gt; { throw ex; } ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload