Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Proposal: withoutErrors() #138

Open
appsforartists opened this issue Jan 15, 2015 · 7 comments
Open

Feature Proposal: withoutErrors() #138

appsforartists opened this issue Jan 15, 2015 · 7 comments

Comments

@appsforartists
Copy link
Contributor

Consider this code:

return this.getFromAPI("things").then(
  modelsFromAPI =>  Lazy(modelsFromAPI).map(
                      apiModel => MyModel.fromAPI(
                                    apiModel,
                                  )
                    ).toArray()
);

I don't know that what I get back from the API will always be sane, but I don't want to throw away 99 good models because 1 is invalid. Especially for prototypes and demos, it would be great if I could add withoutErrors to the chain, which would try to map each model, catch errors, and compact them away. Hence, if I had 99 good passes and 1 bad, I'd get back an array of 99 elements (and a console.error call explaining the missing one).

@olsonpm
Copy link
Contributor

olsonpm commented Jan 19, 2015

return this.getFromAPI("things")
  .then(function(modelsFromAPI) {
    return Lazy(modelsFromAPI).map(function(apiModel) {
      return MyModel.fromAPI(apiModel);
    }).toArray();
  });

I rewrote your given code so I could understand it better. I'm still confused at what "return MyModel.fromAPI(apiModel)" is conceptually doing - would you mind explaining a little deeper? I think I get the overall problem you're trying to solve, but I was trying to think of a temporary workaround (assuming you haven't found one already) and it's easier to find a workaround given a concrete example.

edit: nvm - give me another moment. I realize I initially read the pseudo-code incorrectly.

@olsonpm
Copy link
Contributor

olsonpm commented Jan 19, 2015

The below code is what you're after correct?

I'm not a maintainer but personally I've been adding extensions to a separate file (e.g. lazy-extensions.js) then requiring that file instead. This would at least enable you to use this functionality everywhere without having to write the below function each time.

return this.getFromAPI("things")
  .then(function(modelsFromAPI) {
    var res = [];
    Lazy(modelsFromAPI).each(function(apiModel) {
      try {
        var tmpModel = MyModel.fromAPI(apiModel);
        res.push(tmpModel);
      } catch (err) {
        // log error
      }
    });
    return res;
  });

@appsforartists
Copy link
Contributor Author

Effectively, yes. It's not hard to write, but it adds clutter that makes it harder to understand what's going on in the non-error case. It would be awesome if there was something like .compact that ignored any iteration that threw an error. In fact, maybe it should be called compactErrors.

I could certainly have my own version of lazy-extensions, but this feature should be useful in the general case, not just for me. 😃

@olsonpm
Copy link
Contributor

olsonpm commented Jan 19, 2015

Agreed - I also think it would be cool to pass in an array of error types which you deem as 'okay' to keep iterating, and if other unexpected errors happen to escape iteration per-normal. From the javascript source code I come across though - specific error types are a rarity x.x

@appsforartists
Copy link
Contributor Author

I'd be cool with that.

@dtao
Copy link
Owner

dtao commented Jan 29, 2015

Let me think about this. I'm certainly not opposed to the idea, but I'm not 100% sure I want to include it in "core" Lazy.js either. The truth is I've been thinking about slimming Lazy down and relegating some of its functionality to some sort of extension, and then putting some love into the library's extensibility in general.

@olsonpm: I'd love to chat with you about how you've been extending the library for your own purposes, and maybe bounce some ideas off of you about what the API for customizing and making your own helpers and sequence types would look like. Is that something you'd be interested in?

@olsonpm
Copy link
Contributor

olsonpm commented Jan 29, 2015

For sure. And the hard part would definitely be defining what belongs in the core library, what belongs in a 'core' extension, and what you should leave people to write on their own.

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

No branches or pull requests

3 participants