Skip to content

Allow promises to be returned in lifecycle hooks #177

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

Merged
merged 1 commit into from
Sep 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dist/angular-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7101,7 +7101,10 @@ module.exports = [function () {
});

try {
fn.apply(target || this, args);
var promise = fn.apply(target || this, args);
if(promise && promise.then){
promise.then(deferred.resolve, deferred.reject);
}
} catch (err) {
deferred.reject(err);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-data.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@
});

try {
fn.apply(target || this, args);
var promise = fn.apply(target || this, args);
if(promise && promise.then){
promise.then(deferred.resolve, deferred.reject);
}
} catch (err) {
deferred.reject(err);
}
Expand Down
81 changes: 81 additions & 0 deletions test/integration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe('$q decorator', function(){
beforeEach(module('angular-data.DS'));

it('should decorate $q', inject(function($q){
assert.isFunction($q.promisify);
}));

it('should resolve with a cb', inject(function($q, $rootScope){
var resolveValue = {};
var resolveCb = function(cb){
cb(null, resolveValue);
};
var resolveSpy = sinon.spy();

$q.promisify(resolveCb)().then(resolveSpy);
$rootScope.$digest();

assert(resolveSpy.calledWith(resolveValue));
}));

it('should reject with a cb', inject(function($q, $rootScope){
var rejectValue = {};
var rejectCb = function(cb){
cb(rejectValue);
};
var rejectSpy = sinon.spy();

$q.promisify(rejectCb)().then(null, rejectSpy);
$rootScope.$digest();

assert(rejectSpy.calledWith(rejectValue));
}));

it('should resolve with a promise', inject(function($q, $rootScope){
var resolveValue = {};
var resolveCb = function(cb){
return $q.when(resolveValue);
};
var resolveSpy = sinon.spy();

$q.promisify(resolveCb)().then(resolveSpy);
$rootScope.$digest();

assert(resolveSpy.calledWith(resolveValue));
}));

it('should reject with a promise', inject(function($q, $rootScope){
var rejectValue = {};
var rejectCb = function(cb){
return $q.reject(rejectValue);
};
var rejectSpy = sinon.spy();

$q.promisify(rejectCb)().then(null, rejectSpy);
$rootScope.$digest();

assert(rejectSpy.calledWith(rejectValue));
}));

//Typically, functions that return a promise will be wrapped with a $q.when, meaning if you were to return nothing, it would execute straight away
//This would mean the cb style would not work at all, as any developer that uses cb would have the function immediately resolve
//This just ensures that doesn't ever happen
it('should not resolve or reject if return value is not a promise', inject(function($q, $rootScope){
var resolve;
var cb = function(next){
resolve = next;
return true;
}
var spy = sinon.spy();

$q.promisify(cb)().finally(spy);
$rootScope.$digest();

assert(!spy.called);

resolve();
$rootScope.$digest();

assert(spy.called);
}));
})