jlongster / transducers.js

A small library for generalized transformation of data (inspired by Clojure's transducers)
BSD 2-Clause "Simplified" License
1.72k stars 54 forks source link

Error handling #28

Closed zeroware closed 9 years ago

zeroware commented 9 years ago

How do you handle errors that are happening inside a transformation ? I wonder if simple throw can work with ES6 generators.

stefanpenner commented 9 years ago

throw should work as expected with generators.

jlongster commented 9 years ago

If you are using this against arrays/objects/iterators, it's happening immediately so the error is thrown within the caller that is requesting the information, so you can catch it there:

try {
  t.seq(iter, t.map(x => garbage()));
}
catch(e) {
  // ...
}

Generators are actually producers of values, and it makes more sense from the consumer to care about values. If you are using transducers with other stuff (like async), it's up to the async library to figure out. The async lib just needs to run the transform within a try/catch and do something with it.