No,
futures are not equivalent to
observables.
observables are related to "event streams".
futures and
promises deal with single asynchronous operations.
If you consider the
webSocket stuff then we offer a read operation, that can will perform one read operation which will complete some time in the future.
In a system with
observables we would have implemented the read operations in a predesigned loop and you would then observe a stream of
webSocket message events.
It seems that many people have faith in
Reactive Extensions (ReactiveX).
=== Begin prejudice
===
I am however not convinced, because I think it seems to repeat a problem that has been seen before in other "event" contexts (for example SAX parsing of XML documents).
The problem arise when you somehow need to "merge" the results of several (semi-)independent event sources (i.e.
observables).
As the simplest possible example let us simply imagine that that we have two event sources whose events will always match-up in pairs; you need one of each to create a "merge".
In synchronous code you could imagine this (oversimplified) loop:
Code: Select all
clauses
loop(S1, S2) :-
E1 = S1:read(),
E2 = S2:read(),
merge(E1, E2),
loop(S1, S2).
S1 and
S2 are the "event sources" (for example a
webSocket).
Such a loop can without much problems made asynchronous using
await and/or
futures.
It is however quite difficult to attach observers to S1 and S2 which can perform such a merge. The observers are too independent of each other; and thus very difficult to "synchronize" with each other.
ReactiveX library have several kinds of operations that performs "merge/synchronize" operations. The loop above corresponds to a
zip of the two event sources.
Consider this slight modification:
Code: Select all
clauses
loop(S1, S2) :-
E1a = S1:read(),
E1b = S1:read(),
E2 = S2:read(),
merge(E1a, E1b, E2),
loop(S1, S2).
The code is still very simple and understandable: we combine two pieces of data from
S1 with one from
S2
I do not know how ReactiveX code corresponding to this looks like (if it exists), but I fear that it is not as easy and straightforward as the modification I made.
=== End prejudice ===