Search code examples
javascriptfrpbacon.js

Bacon.when not matching streams as expected


In experimenting with Bacon.js, I've come across the following unexpected behavior:

    var email = $("#email")
                            .asEventStream("keyup")
                            .map(function(event) {
                                return $(event.target).val();
                            })
                            .log();

    var validEmail = email.map(validateEmail).log();

    // submit.doAction('.preventDefault'); isn't working for some reason
    $('form').on('submit', function (event) { event.preventDefault() });

    var submit = $('form').asEventStream('submit');

    // postFormData is never called
    Bacon.when([email, validEmail, submit], postFormData);  

Each of the streams emits values appropriately, but the join pattern is never matched.

Fiddle


Solution

  • You never do anything with the result of Bacon.when, so lazy evaluation causes postFormData never to be called.

    As a rule of thumb, something that causes side effects, like posting the data to server, should be done in an onValue handler. Combining the different EventStreams should be done using pure functions.

    I updated your fiddle by adding a .log at the end.