Promise based Observable proposal

Yad Smood y.s.inside at gmail.com
Mon May 9 07:48:27 UTC 2016


Hi everyone,

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
<https://jsbin.com/niwuti/edit?html,js,output>.

The implementation is here:
https://github.com/ysmood/yaku#observableexecutor

Code example:

```js
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160509/a5c1501b/attachment.html>


More information about the es-discuss mailing list