Search code examples
javascriptpromisedeferredfrpbacon.js

How can I express the initial processes by Bacon.js in place of Promise?


I alway do something like this for initial processes:

$.when(dom(), webrtc(), websocket('wss://example.com/'), sleep(3000)).then(load, fail);

dom, webrtc, websocket, and sleep are Promise objects. This expression is useful to load some parallel processes for initiation.

Now I am wondering how I can express these things by Bacon.js, a way of functional reactive programming.

Any idea would be appreciated. Thanks in advance.


Solution

  • First of all, it's perfectly fine to mix and match promises into your BaconJS code. That said, given the abstraction BaconJS has a fromPromise method.

    Use Bacon.fromPromise:

    var ready = Bacon.fromPromise($.when(dom(), 
                                         webrtc(),
                                         websocket('wss://example.com/'),
                                         sleep(3000)))
    
    ready.onValue(function(value){
        console.log("All ready");
    });
    

    Note that the power here is in combining these streams, in the initialization phase - you rarely need this so I'd probably stick to a promise if I were you.