Promise based Observable proposal
# Isiah Meadows (9 years ago)
Probably best to first see how people react here: zenparsing/es-observable
You also might appreciate the async generator/iterator proposal here: tc39/proposal-async-iteration
(Note: observables push directly to iterators and are event based, while async iterators are effectively iterators of promises, and they are functionally equivalent, but they are two different ways of looking at the same problem. Observables are better with single events, while async iterators are like normal iterators. They are both heavily promise-based.)
I've been working on implementing a Promise based Observable proposal.
The main goal is to make the Observable composable, every subscribe will return a new Observable, and every listener will try to resolve the returned value as a Promise. Promise can't resolve multiple times, this class makes it possible, so that you can easily map, filter and even back pressure events in a promise way.
For live example: Double Click Demo jsbin.com/niwuti/edit?html,js,output.
The implementation is here: ysmood/yaku#observableexecutor
Code example:
var Observable = require("yaku/lib/Observable"); var linear = new Observable(); var x = 0; setInterval(linear.next, 1000, x++); // Wait for 2 sec then emit the next value. var quad = linear.subscribe(async x => { await sleep(2000); return x * x; }); var another = linear.subscribe(x => -x); quad.subscribe( value => { console.log(value); }, reason => { console.error(reason); } ); // Emit error linear.error(new Error("reason")); // Unsubscribe an observable. quad.unsubscribe(); // Unsubscribe all subscribers. linear.subscribers = [];
The lib itself is on a very early stage, please leave a comment if you have any idea on it.