by slackingoff2017 · 9y ago · 0 comments
The vast majority of observer pattern use cases are solved a lot more clearly by using async
I know some not bad js developers who use RX a lot and have no idea about `async/await` existence, I even had to explain to couple of them how `async/await` works and what is "imperative code". No joke.
Each has very different strengths, and it's very easy to work with both together.
const someStream = Rx.fromPromise(myAsyncAwaitFunction())
const arrayINeed = await myRxObservable.toArray().toPromise()Vs
Observable.of(thing).subsribe((res)=>{call back}
For all(most) cases where you only need to hear back once
But again, it's a false dichotomy, and your second example isn't much different from what await essentially rewrites to:
Thing().then(val => /* call back */)
The benefit to async/await is that the runtime does the rewriting to Promise callbacks for you. But you can use both and get that advantage for both. Rx has supported converting to/from Promises since the beginning. Working with both is easy. let val = await observableOfJustOneThingINeed
.take(1) // I just need one, I don't care if there are more
.toPromise()
You don't have to manually subscribe, you can use a Promise and await it.