Observable.FromAsync vs Task.ToObservable

user630190 picture user630190 · Jan 21, 2016 · Viewed 9.6k times · Source

Does anyone have a steer on when to use one of these methods over the other. They seem to do the same thing in that they convert from TPL Task to an Observable.

Observable.FromAsync appear to support cancellation tokens which might be the subtle difference that allows the method generating the task to participate in cooperative cancellation if the observable is disposed.

Just wondering if I'm missing something obvious as to why you'd use one over the other.

Thanks

Answer

Sickboy picture Sickboy · Aug 20, 2016

Observable.FromAsync accepts a TaskFactory in the form of Func<Task> or Func<Task<TResult>>, in this case, the task is only created and executed, when the observable is subscribed to.

Where as .ToObservable() requires an already created (and thus started) Task.