Search code examples
c#typescriptpromiseobservabletask

Angular counterpart of C# Tasks


Hello coming from C# I am struggling a bit to understand which is what when it comes to typescript.

So a Promise in Typescript would be the equivalent of a C# Task and while in C# I unwrap it using await in the latter I access the data inside the context with subscribe ?

What is the relationship between Promise and Observable. So far I understood that Observable class behaves like the one in System.Reactive but where does Promise come in this equation?

Why do I have the toPromise extension method defined for an Observable? I am trying to do a parallel with C# and you can't convert an IEnumerable/IObservable to a Task.

How does that even make sense? I have a stream. Why would I await it? There might be no ending to that stream. I would understand if you await an element of the Observable but why on the whole?


Solution

  • Promise and Observable are the same only with respect them both being tools for modeling async operations.

    Their main difference is that Observable operates on stream of events, thus it has array-like operators while, on the other hand, Promise is an one time stop when it comes to async operations > once async operation finishes (or fails), your Promise is done and cannot be used anymore.