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

would it be sufficient if $parse took an optional argument that would tell it to not unwrap promises?

IgorMinar avatar Sep 27 '13 20:09 IgorMinar

@jankuca your PR #3681 could handle this in an easier way than the current closure based code. could you take a look at this and create a commit on top of that PR that would implement the promise unwrapping flag?

IgorMinar avatar Sep 27 '13 20:09 IgorMinar

Sure, I'm going to look into it.

jankuca avatar Sep 27 '13 20:09 jankuca

Sure, an optional argument would solve the problem, though to me opting in to promise unwrapping would make more sense than opting out.

wilsonjackson avatar Sep 27 '13 20:09 wilsonjackson

@wilsonjackson fair enough. we'll consider it

IgorMinar avatar Sep 27 '13 20:09 IgorMinar

I mentioned this in my PR that this issue stemmed from, but please make sure that functions returning promises are processed the same way by $parse as promise objects in scope. I don't mind unwrapping being opt-in, but keep in mind that promise objects have been unwrapped automatically for a long time now.

jussik avatar Sep 29 '13 18:09 jussik

@IgorMinar an optional opt-in argument would be a perfect solution. We really need it badly for the angular-ui/bootstrap. @jankuca if I help anyhow I could give you a hand with this issue, I'm really keen on doing anything that would speed up this issue resolution.

pkozlowski-opensource avatar Sep 29 '13 18:09 pkozlowski-opensource

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

I'd argue that promise unwrapping is an "extra" feature that is not intrinsically related to parsing, so it would make the most sense for it not to happen by default. Also, the time to introduce breaking changes is now. I think this is the perfect time to correct this slight overreach in $parse's implementation.

wilsonjackson avatar Sep 29 '13 19:09 wilsonjackson

I did some work regarding this issue: https://github.com/jankuca/angular.js/compare/dddc774...promise_unwrapping

But we need to get #3681 first.

jankuca avatar Sep 30 '13 17:09 jankuca

I'll second that this is a pretty big breaking change and it doesn't allow for knowing when the promise is resolved for actions.

And now I'm between a rock and a hard place, as RC1 has broken ngIncludes and RC2 has this broken :S

Does anyone know what direction you'll take after all so I can at least work around only one of the issues?

georgiosd avatar Oct 01 '13 18:10 georgiosd

Also note that this affects the behavior of directive & bindings and any options you add need to be able to be applied there too...

georgiosd avatar Oct 01 '13 18:10 georgiosd

Agreed @georgiosd . Spent about half a day debugging this.

ericpanorel avatar Oct 02 '13 19:10 ericpanorel

How about we move the promise unwrapping into the attributeInterpolationDirective, since that is really the only place where it should be expected to automatically unwrap? If you are working with promises in code then you should have full control.

petebacondarwin avatar Oct 03 '13 06:10 petebacondarwin

@petebacondarwin I'm unfamiliar with the attributeInterpolationDirective, would that work transparently with ng-repeat="entry in promise"?

Another thing that might be worth looking into is having the unwrapping option stored in the promise itself. So if we want the promise to be unwrapped we have to flip an opt-in property before it resolves (or at least before the next $parse).

jussik avatar Oct 03 '13 07:10 jussik

Another idea: define an $unwrap() function and let the devel. take full control. Lean, simple to understand.

xrg avatar Oct 03 '13 07:10 xrg

@jussik I don't like the idea of the promise itself having to be modified. The concern of whether a promise is unwrapped or not belongs entirely to the code initiating a parse. A scope function that returns a promise should make no assumption about how that promise will be used.

wilsonjackson avatar Oct 03 '13 16:10 wilsonjackson

I like @xrg 's idea - leave it to the programmer. One less bit of magic is probably good.

georgiosd avatar Oct 03 '13 16:10 georgiosd

Some of the suggestions here, when contrasted with the current behavior, bring up an interesting point. Whereas an $unwrap service or similar would only be able to unwrap a promise if it's the end result of an expression (e.g., returnPromise()), the current behavior will unwrap a promise resulting from a function call at any point within an expression (e.g., scopeVar = returnPromise()") or even more insidious consumePromise(returnPromise())).

That being the case, I don't see how anything but a flag passed to $parse would allow compatibility with current behavior to be maintained. Whether or not retaining compatibility for use cases such as those is a good idea I leave for others to debate. I only care about being given the option of not auto-unwrapping promises.

wilsonjackson avatar Oct 03 '13 22:10 wilsonjackson

An $unwrap() /function/ would cover all of the above cases AFAICT, with really visible logic. The original idea had been that we want to unwrap whenever it's the snapshot value (rather than the blocking result) we are interested in, right?

So, we could write: currentVal = unwrap(somePromise()) or consumeValue(unwrap(somePromise())) having a full manual control of where a promise and where its value is used. Note that we would have to expose unwrap to the scope of parsing (or should it be always defined? ).

xrg avatar Oct 04 '13 07:10 xrg

Doesn't all this defeat the point of auto-unwrapping, which is that it is syntactic sugar to make templates more succinct? If you have a promise then programmatic control is there already, you just use a controller with a then() call to assign it to the scope property.

Either unwrapping happens without any intervention on the template writer's part or it should be ditched altogether. Personally I prefer the latter since unwrapping promises hides the asynchronicity.

In any case, $parse should not unwrap promises automatically.

On 4 October 2013 08:07, xrg [email protected] wrote:

An $unwrap() /function/ would cover all of the above cases AFAICT, with really visible logic. The original idea had been that we want to unwrap whenever it's the snapshot value (rather than the blocking result) we are interested in, right?

So, we could write: currentVal = unwrap(somePromise()) or consumeValue(unwrap(somePromise())) having a full manual control of where a promise and where its value is used. Note that we would have to expose unwrap to the scope of parsing (or should it be always defined? ).

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

petebacondarwin avatar Oct 04 '13 07:10 petebacondarwin