Skip to content

Commit

Permalink
fix($parse): handle promises returned from parsed function calls
Browse files Browse the repository at this point in the history
When a parsed function call returns a promise, the evaluated value
is the resolved value of the promise rather than the promise object.

Closes angular#3503
  • Loading branch information
jussik authored and vojtajina committed Aug 15, 2013
1 parent 37123cd commit 3a65822
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,21 @@ function parser(text, json, $filter, csp){
}
var fnPtr = fn(scope, locals, context) || noop;
// IE stupidity!
return fnPtr.apply
var v = fnPtr.apply
? fnPtr.apply(context, args)
: fnPtr(args[0], args[1], args[2], args[3], args[4]);

// Check for promise
if (v && v.then) {
var p = v;
if (!('$$v' in v)) {
p.$$v = undefined;
p.then(function(val) { p.$$v = val; });
}
v = v.$$v;
}

return v;
};
}

Expand Down
12 changes: 12 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,18 @@ describe('parser', function() {
expect(scope.$eval('greeting')).toBe(undefined);
});

it('should evaluate a function call returning a promise and eventually get its return value', function() {
scope.greetingFn = function() { return promise; };
expect(scope.$eval('greetingFn()')).toBe(undefined);

scope.$digest();
expect(scope.$eval('greetingFn()')).toBe(undefined);

deferred.resolve('hello!');
expect(scope.$eval('greetingFn()')).toBe(undefined);
scope.$digest();
expect(scope.$eval('greetingFn()')).toBe('hello!');
});

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

0 comments on commit 3a65822

Please sign in to comment.