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

Support for error handling and promises #3

Merged
merged 4 commits into from
Jul 30, 2014

Conversation

izaakschroeder
Copy link
Contributor

Now you can have error handling in your callbacks (more in-line with standard node callbacks); you can also use promises in the same fashion gulp does which makes some patterns simpler. Appropriate tests have been added. Unfortunately this breaks previous data providers, but data consumers are not effected. Some examples of the new style follow.

Generate an error:

data(function(cb) { cb('error') });

Return a successful result:

data(function(cb) { cb(undefined, { hello: 'World' }) });

Use a promise from MongoDB:

data(collection.findOne({ tag: 'test' }))

Use a promise from MongoDB within the callback:

data(function(file) { 
    return collection.find({ filename: path.basename(file.path) }); 
})

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

This is a really great contribution! What do you mean by data consumers? You mean like gulp-swig and gulp-jade? So they can still depend on file.data being available as a regular object?

@izaakschroeder
Copy link
Contributor Author

Correct! file.data remains the same as far as things like gulp-swig and gulp-jade are concerned.

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

Also, I see what you mean about the breaking change on the setup side now that it includes the err object as the first param. I'm not sure of the best way to handle that yet.

@izaakschroeder
Copy link
Contributor Author

Major version bump due to API difference; so people who specify gulp-data as a dependency (e.g. gulp-data: "~1.0.1") don't automatically have the new version kill all their stuff.

You could type check the first object; arg1 instanceof Error results in the error handling logic being called, but that's flaky since some people trigger errors that don't include Error in the prototype chain.

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

yeah that's reasonable... the work you did justifies it being out of "beta" as it's really well rounded now. As it's a major contribution, would you mind updating the version as well as adding your self as a contributor in the package.json? Once that's committed I'll merge it in. Thanks!

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

oh, can you also update the README with your examples? :-D

… one more feature for returning objects in data function.
@izaakschroeder
Copy link
Contributor Author

Done I think. Final feature: you can return the data object from the data function as well now.

data(function(file) {
   return { foo: 'bar' };
});

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

Very impressive, thanks for your help!

colynb added a commit that referenced this pull request Jul 30, 2014
Support for error handling and promises
@colynb colynb merged commit 61a8b15 into colynb:master Jul 30, 2014
@izaakschroeder
Copy link
Contributor Author

No problem! Let me know when it's upstream in npm.

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

All published... I assumed the contributors section in the package file would get your name to show up in npm, but it's not. I'll figure it out.

@colynb
Copy link
Owner

colynb commented Jul 30, 2014

nevermind, I guess it doesn't do that. I added the list to the README. Thanks again.

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

Successfully merging this pull request may close these issues.

None yet

2 participants