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

utils: add makeNodeResolver function. #488

Merged
merged 5 commits into from
Dec 4, 2014

Conversation

jamestalmage
Copy link
Contributor

Many Firebase methods take Node.js style callbacks where the first
argument is null do indicate success or contains error information.
Angular uses promises to handle asyncronous flow. The original
Q library by kriskowal (upon which Angular promises are based)
had a makeNodeResolver function that eased integration of the two
different async flows.

This utility function replicates that feature. Usage is as follows:

var defer = $q.defer();
ref.resetPassword(
  {email:'somebody@somewhere.com'},
  $util.makeNodeResolver(defer) //automatically resolves/rejects promise
);

defer.promise.then(...) // use the promise as you normally would

Many Firebase methods take Node.js style callbacks where the first
argument is null do indicate success or contains error information.
Angular uses promises to handle asyncronous flow. The original
[Q](https://github.com/kriskowal/q) library by kriskowal (upon which
had Angular promises are based) had a `makeNodeResolver` function
that eased integration of the two different async flows.

This utility function replicates that feature. Usage is as follows:

```javascript
var defer = $q.defer();
ref.resetPassword(
  {email:'somebody@somewhere.com'},
  $util.makeNodeResolver(defer) //automatically resolves/rejects promise
);

defer.promise.then(...) // use the promise as you normally would
```
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling d6f09ba on jamestalmage:utils-node-resolver into * on firebase:master*.

@jamestalmage
Copy link
Contributor Author

a lot of angularFire functions can benefit from this (almost everything in FirebaseAuth will)

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling afc3fe7 on jamestalmage:utils-node-resolver into * on firebase:master*.

@jamestalmage
Copy link
Contributor Author

Seems FirebaseAuth is the only place you are using promises, so those are the only internal methods that benefit. I say it still belongs on the utility object, but I can move it to FirebaseAuth if you want.

@jamestalmage
Copy link
Contributor Author

I did an offline merge and the refactoring still passes the new FirebaseAuth test suite.

expect(deferred.reject).toHaveBeenCalledWith(error);
});

it('should resolve the promise if the first argument is falsy', function(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would like to see these methods specifically check for null. Node, and the Firebase methods, both pass null for the error if it is a success, so anything else would be fishy and either a bug or error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense. They don't do that check in the original library, but according to the Node.js spec, error should be null. I've made the change

var result2 = {data:'howdy!'};
callback(null,result1,result2);
expect(deferred.resolve).toHaveBeenCalledWith([result1,result2]);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added this as well. ^

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.04%) when pulling 5d58be7 on jamestalmage:utils-node-resolver into 38c509f on firebase:master.

katowulf added a commit that referenced this pull request Dec 4, 2014
utils: add makeNodeResolver function.
@katowulf katowulf merged commit b512baa into FirebaseExtended:master Dec 4, 2014
@jamestalmage jamestalmage deleted the utils-node-resolver branch December 4, 2014 22:44
@katowulf
Copy link
Contributor

katowulf commented Dec 4, 2014

-0.04%!! There goes the farm.

@jwngr
Copy link

jwngr commented Dec 5, 2014

This is super useful and great addition. 👍

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.

4 participants