Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3a65822

Browse files
jussikvojtajina
authored andcommitted
fix($parse): handle promises returned from parsed function calls
When a parsed function call returns a promise, the evaluated value is the resolved value of the promise rather than the promise object. Closes #3503
1 parent 37123cd commit 3a65822

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/ng/parse.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,21 @@ function parser(text, json, $filter, csp){
689689
}
690690
var fnPtr = fn(scope, locals, context) || noop;
691691
// IE stupidity!
692-
return fnPtr.apply
692+
var v = fnPtr.apply
693693
? fnPtr.apply(context, args)
694694
: fnPtr(args[0], args[1], args[2], args[3], args[4]);
695+
696+
// Check for promise
697+
if (v && v.then) {
698+
var p = v;
699+
if (!('$$v' in v)) {
700+
p.$$v = undefined;
701+
p.then(function(val) { p.$$v = val; });
702+
}
703+
v = v.$$v;
704+
}
705+
706+
return v;
695707
};
696708
}
697709

test/ng/parseSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,18 @@ describe('parser', function() {
846846
expect(scope.$eval('greeting')).toBe(undefined);
847847
});
848848

849+
it('should evaluate a function call returning a promise and eventually get its return value', function() {
850+
scope.greetingFn = function() { return promise; };
851+
expect(scope.$eval('greetingFn()')).toBe(undefined);
852+
853+
scope.$digest();
854+
expect(scope.$eval('greetingFn()')).toBe(undefined);
855+
856+
deferred.resolve('hello!');
857+
expect(scope.$eval('greetingFn()')).toBe(undefined);
858+
scope.$digest();
859+
expect(scope.$eval('greetingFn()')).toBe('hello!');
860+
});
849861

850862
describe('assignment into promises', function() {
851863
// This behavior is analogous to assignments to non-promise values

0 commit comments

Comments
 (0)