Skip to content

Instantly share code, notes, and snippets.

@flash42
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flash42/4694896467562555d070 to your computer and use it in GitHub Desktop.
Save flash42/4694896467562555d070 to your computer and use it in GitHub Desktop.
Can we find a better way for shutting down polling and throwing errors in Bacon.js?
Unsubscribe from polling with ugly unsubBus

I've implemented polling with unsubBus "pattern". Can you suggest something better? Pseudo code:

var unsubBus = new Bacon.Bus();
// Poll job status code until it's successful
var succStream = Bacon.interval(pollInterval)
    .takeUntil(unsubBus)
    .flatMapLatest(function (v) { return this.checkStatus(jobId); })
    .filter(function(v) { return v === true; });

// Unsubscribe and do something
succStream.onValue(function (_) { unsubBus.push(true); });
succStream.onValue(function (_) { DO-SOMETHING; });

Running example: http://jsfiddle.net/o8nnmswa/

Signaling errors on stream with nasty trick

I couldn't find a nice way to "throw" errors to handle them later. It would be cool to have a succint syntax for the following:

var streamWithPossibleErrors = bus.flatMap(function(v) {
    if (v != "bacon") {
        return Bacon.sequentially(0, [new Bacon.Error("Error to be reported later")])
    }
    return v
});

Running example: http://jsfiddle.net/aejfyf7t/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment