1

I want to use bacon.js for the following scenario:
1. read files in a directory
2. each file consists of a url, make http request to the url
3. write each response into a corresponding file in another directory

From the docs, I understand that the errors from one stream are passed onto the subsequent ones, even if all values are filtered out. If this is the case, then I only need to specify the onError handler on the last stream, since it will receive errors from the previous streams as well. Is that correct?

Also, it seems that by default, an error event doesn't terminate the stream. So even if one file operation or http request fails, the others are unaffected. Is that correct?

tldr
  • 11,924
  • 15
  • 75
  • 120

1 Answers1

1

Yes, you are correct. Basically, if you attach an onValue handler to a stream, that is the stream you want to attach the onError handler to too.

Something like this:

var files = ["a.txt", "b.txt", "c.txt"]
var filenames = Bacon.fromArray(files)
var urls = filenames.flatMap(readFile)
var responses = url.flatMap(doHttpRequest)

responses.onValue(writeToFile)
responses.onError(handleError)

// readFile & doHttpRequest are async, so they return a stream
// that's why we need the flatMap above
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
OlliM
  • 7,023
  • 1
  • 36
  • 47
  • So the standard transformations (map, filter etc) are only applied to 'values', and the 'errors' just pass through unaffected? My understanding is that 'errors' are only processed by the onError(f) and endOnError(f) callbacks. Is that correct? – tldr Sep 11 '14 at 14:58
  • Yes, that is correct. You might also be interested in the [mapError](https://github.com/baconjs/bacon.js/#observable-maperror) function: you can use it to map errors to other error or to values. – OlliM Sep 12 '14 at 10:20
  • If you look at the bacon.js source code, you'll see a pattern of `this.filter(false).merge(stream)`: it's used to replace the contents of the current stream completely, but let the errors pass through. Eg. https://github.com/baconjs/bacon.js/blob/master/src/Bacon.coffee#L784 – OlliM Sep 12 '14 at 10:23