1

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.

Japboy
  • 2,699
  • 5
  • 27
  • 28

1 Answers1

2

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.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Thanks for your answer. Yes, initialisation rarely needs to be called though. I'm just wondering if I can remove dependency to Promise and only use Bacon instead, or not... – Japboy Jul 01 '14 at 06:27
  • 1
    Why though? Having different abstractions, each where appropriate is a good thing :) – Benjamin Gruenbaum Jul 01 '14 at 08:09