Skip to content

Commit 5f4b501

Browse files
committed
Merge pull request #177 from clark-pan/master
Allow promises to be returned in lifecycle hooks
2 parents 129b6b2 + 093a7f4 commit 5f4b501

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

dist/angular-data.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -7101,7 +7101,10 @@ module.exports = [function () {
71017101
});
71027102

71037103
try {
7104-
fn.apply(target || this, args);
7104+
var promise = fn.apply(target || this, args);
7105+
if(promise && promise.then){
7106+
promise.then(deferred.resolve, deferred.reject);
7107+
}
71057108
} catch (err) {
71067109
deferred.reject(err);
71077110
}

dist/angular-data.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@
7171
});
7272

7373
try {
74-
fn.apply(target || this, args);
74+
var promise = fn.apply(target || this, args);
75+
if(promise && promise.then){
76+
promise.then(deferred.resolve, deferred.reject);
77+
}
7578
} catch (err) {
7679
deferred.reject(err);
7780
}

test/integration/index.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
describe('$q decorator', function(){
2+
beforeEach(module('angular-data.DS'));
3+
4+
it('should decorate $q', inject(function($q){
5+
assert.isFunction($q.promisify);
6+
}));
7+
8+
it('should resolve with a cb', inject(function($q, $rootScope){
9+
var resolveValue = {};
10+
var resolveCb = function(cb){
11+
cb(null, resolveValue);
12+
};
13+
var resolveSpy = sinon.spy();
14+
15+
$q.promisify(resolveCb)().then(resolveSpy);
16+
$rootScope.$digest();
17+
18+
assert(resolveSpy.calledWith(resolveValue));
19+
}));
20+
21+
it('should reject with a cb', inject(function($q, $rootScope){
22+
var rejectValue = {};
23+
var rejectCb = function(cb){
24+
cb(rejectValue);
25+
};
26+
var rejectSpy = sinon.spy();
27+
28+
$q.promisify(rejectCb)().then(null, rejectSpy);
29+
$rootScope.$digest();
30+
31+
assert(rejectSpy.calledWith(rejectValue));
32+
}));
33+
34+
it('should resolve with a promise', inject(function($q, $rootScope){
35+
var resolveValue = {};
36+
var resolveCb = function(cb){
37+
return $q.when(resolveValue);
38+
};
39+
var resolveSpy = sinon.spy();
40+
41+
$q.promisify(resolveCb)().then(resolveSpy);
42+
$rootScope.$digest();
43+
44+
assert(resolveSpy.calledWith(resolveValue));
45+
}));
46+
47+
it('should reject with a promise', inject(function($q, $rootScope){
48+
var rejectValue = {};
49+
var rejectCb = function(cb){
50+
return $q.reject(rejectValue);
51+
};
52+
var rejectSpy = sinon.spy();
53+
54+
$q.promisify(rejectCb)().then(null, rejectSpy);
55+
$rootScope.$digest();
56+
57+
assert(rejectSpy.calledWith(rejectValue));
58+
}));
59+
60+
//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
61+
//This would mean the cb style would not work at all, as any developer that uses cb would have the function immediately resolve
62+
//This just ensures that doesn't ever happen
63+
it('should not resolve or reject if return value is not a promise', inject(function($q, $rootScope){
64+
var resolve;
65+
var cb = function(next){
66+
resolve = next;
67+
return true;
68+
}
69+
var spy = sinon.spy();
70+
71+
$q.promisify(cb)().finally(spy);
72+
$rootScope.$digest();
73+
74+
assert(!spy.called);
75+
76+
resolve();
77+
$rootScope.$digest();
78+
79+
assert(spy.called);
80+
}));
81+
})

0 commit comments

Comments
 (0)