What is it?
Why is it important?
The concepts here are reusable ways for promises and pub/sub systems to interact in a healthy way.
Publish and subscribe and promises are two canonical, widely-supported methods of handling asynchronous flow. The ease at which they communicate with each other makes asynchronicity easier to handle.
Promises to observables
Publishing to an observable when a promise completes is trivial. For example,
given a promise
and an observable
When the promise completes it passes the promised value to the observable.
something is published
When something is published, one often wants to fulfill a promise (once). Here is a way to do that.
Here is the scenario. You have an observable
, you want to do something
when it is given (or has) a value or meets a test. Like this:
or an arbitrary test
Here is a jsFiddle in Coffeescript with a function,
that accomplishes this:
For convenience, here is an equivalent
Of course this just scratches the surface, but I hope this highlights the conceptual elements of publish and subscribe and promises.
I hope you find the above useful, and please feel free to comment.