1

I have an array of urls I want to make get requests to. I create a stream from the urls array, and flatMap ajax requests, like so: responses = Bacon.fromArray(url_arr) .flatMap(function(url) {Bacon.fromPromise($.get(url))})

Now, in the responses stream, I want to keep redoing the ajax until I get the value. How do I do that?

tldr
  • 11,924
  • 15
  • 75
  • 120

2 Answers2

0

You could use recursion like this:

var pollUrl = function(url) {
  return Bacon.fromPromise($.get(url)).flatMap(function(response) {
    if (ok(response)) {
      return Bacon.once("response OK");
    }
    else {
      return pollUrl(url);
    }
  })
}

Bacon.fromArray(url_arr).flatMap(pollUrl);
JJuutila
  • 361
  • 1
  • 4
0

You should use flatMap and Bacon.retry

Bacon.fromArray(url_arr).flatMap(function(url) {
    return Bacon.retry({
        source: function() { return Bacon.fromPromise($.get(url)) },
        retries: 5
    })
}).onValue(function(value) {
    console.log("Done: " + value)
}).onError(function(e) {
    // handle error
})

You can check out this jsFiddle, where I use Math.random() to simulate the ajax requests.

OlliM
  • 7,023
  • 1
  • 36
  • 47