angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Automatic unwrapping of promises by $parse severely limits its usefulness

Open wilsonjackson opened this issue 13 years ago • 45 comments

The behavior introduced by 3a65822, which automatically unwraps promises returned by functions invoked by $parse, prevents any consumer of an expression from handling promise resolution on its own. More pertinent discussion of the problem exists here: #3503

Notably this breaks the typeahead directive in UI bootstrap, as per this issue: angular-ui/bootstrap#949

Here's a simple example of why this change is problematic.

http://plnkr.co/edit/ymxbpR?p=preview

The directive in that plunk executes a function on click (in the same way as ng-click) and expects a promise to be returned, which it will use to decorate the link with loading text.

Because $parse unwraps the promise, the directive has no chance to add its own handlers. If you switch the Angular version to 1.2.0rc1, it works as expected. I don't believe this is an uncommon use case, and certainly not one the framework should prevent from being possible. I hope this can be resolved by the next RC.

wilsonjackson avatar Sep 25 '13 18:09 wilsonjackson

... or it should be ditched altogether. Personally I prefer the latter ...

++ Agreed.

xrg avatar Oct 04 '13 07:10 xrg

I guess @petebacondarwin is right. +1 on ditching :)

georgiosd avatar Oct 04 '13 07:10 georgiosd

+1 on ditching

tanshu avatar Oct 04 '13 08:10 tanshu

Yeah, ditching the whole thing certainly makes the process clearer, which I'm definite in favour of. I'm just concerned about all of the 1.0 and 1.1 code that would break if automatic unwrapping was removed. It would likely be a lot more than the amount that broke by adding support for unwrapping functions that return promises.

jussik avatar Oct 04 '13 09:10 jussik

@jussik The particular changes discussed in this thread were introduced after RC1. Correct me if I'm wrong but I think everyone is talking about ditching that, not previous support.

georgiosd avatar Oct 04 '13 09:10 georgiosd

Actually I would ditch the lot as a breaking change for 1.2 but this probably won't happen. On 4 Oct 2013 10:31, "Georgios Diamantopoulos" [email protected] wrote:

@jussik https://github.com/jussik The particular changes discussed in this thread were introduced after RC1. Correct me if I'm wrong but I think everyone is talking about ditching that, not previous support.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/4158#issuecomment-25686440 .

petebacondarwin avatar Oct 04 '13 09:10 petebacondarwin

I'm personally fine with either ditching all or keeping all (preference leaning towards ditching), but I don't see inconsistent behaviour as a valid solution (hence my PR that instigated this discussion).

As @wilsonjackson said:

I agree that promise objects should be unwrapped (or not) according to the same rules whether they result from a function call or not.

The commits that @jankuca has referenced also affect both loose promise objects and functions that return promises.

jussik avatar Oct 04 '13 09:10 jussik

Would ditching it and keeping existing behavior based on a "compatibility flag" be an acceptable solution for current apps that use this? To be deprecated completely in another major release?

georgiosd avatar Oct 04 '13 11:10 georgiosd

+1 for ditching.

If a user wants promises unwrapped, that can easily be got around by adding a $rootScope.unwrap function as per @xrg's suggestion. It seems like something Angular core (and especially the $parse service) shouldn't concern itself with.

wilsonjackson avatar Oct 04 '13 17:10 wilsonjackson

as @petebacondarwin unwrap() doesn't work for the case of: {{unwrap(some.path.with.promise.somewhere.in.the.middle)}} because unwrap would be called with undefined if there was promise somewhere not-at-the-end of the path.

IgorMinar avatar Oct 04 '13 23:10 IgorMinar

Asynchronous validation broken with Angular 1.2.0-rc2: https://github.com/angular-ui/ui-utils/issues/120

andreas-gruenbacher avatar Oct 06 '13 05:10 andreas-gruenbacher

As a workaround, in rc2 i ve commented out the unwrapping lines in _functionCall

Sent from a device with a small keyboard

On 6 Oct 2013, at 08:55, "Andreas Gruenbacher" [email protected] wrote:

angular-ui/ui-utils#120

— Reply to this email directly or view it on GitHub.

georgiosd avatar Oct 06 '13 06:10 georgiosd

guys, check out #4317

IgorMinar avatar Oct 07 '13 19:10 IgorMinar

Yey, awesome! But, @IgorMinar I can't see those commits in master: https://github.com/angular/angular.js/commits/master

I guess I'm just blind or something is messed up with the git repo....

pkozlowski-opensource avatar Oct 09 '13 20:10 pkozlowski-opensource

I think they broke something and got reverted...

petebacondarwin avatar Oct 09 '13 20:10 petebacondarwin

they are in master now. there was an issue with IE8, but that's fixed now.

IgorMinar avatar Oct 10 '13 20:10 IgorMinar

I still don't see a fix with that or a similar title on master. @IgorMinar, has this really been fixed in a completely different commit?

andreas-gruenbacher avatar Oct 12 '13 22:10 andreas-gruenbacher

This is a great feature!! why to take it down?

AlmogBaku avatar Oct 18 '13 23:10 AlmogBaku

Please consider introducing a custom operator (#, @ or something else) to make promise unwrapping explicit. For example:

  1. ng-options="item.name for item in items#"
  2. {{items#[0].name}}

thorn0 avatar Oct 23 '13 11:10 thorn0

Both cases can be implemented using a function which unwraps the promise (i.e. unwrap(items) instead of items#). The function would be something along the lines of:

if (!('$$v' in promise)) {
    promise.$$v = undefined;
    promise.then(function(val) { promise.$$v = val; });
}
return promise.$$v;

jussik avatar Oct 23 '13 13:10 jussik