Using promises in filters acts strange / crashes my browser

402 views
Skip to first unread message

Nick R

unread,
Apr 2, 2012, 12:06:21 AM4/2/12
to ang...@googlegroups.com
http://jsfiddle.net/KKcaw/1/

Run at your own risk.  It always crashes my browser.

All I wanted to do was make a filter that takes a promise and returns a different promise.

Nick R

unread,
Apr 2, 2012, 1:49:36 AM4/2/12
to ang...@googlegroups.com
Looks like it's an infinite loop.  When the promise resolves, it re-runs the filters, and when the filter's promise resolves it re-runs them again.  This seems to happen any time a filter returns a promise.  Pretty silly.  I think I can break out of this behavior by memoizing my promises externally, but I thought promises were supposed to self-memoize so angular must be breaking something here.

If I pass a promise into a filter, the value the filter sees is 'null'.  Angular re-runs the filter a few times with nulls, and then eventually with the actual data.  I have to guard my filters against null inputs because of this silliness, or they'll end up spouting lots of errors.

So, that's the workaround I'm using for now.  Guard all my filters with if statements until their inputs aren't null, and then memoize the result to prevent an infinite loop.  It would be nice if I could just use promises in my filters though.

Misko Hevery

unread,
Apr 2, 2012, 7:38:54 PM4/2/12
to ang...@googlegroups.com
so the null check is expected, as filter developer you need to do something with the null, perhaps you want to display 'loading ...' so we can't just not call you.

The return of promise is interesting, but again, I think we are doing the right thing, since we can't know that the new promise is same as the old one. After all you may have changed your mind and decided to change the promise.

All filters get called on each $digest.

so while I feel your pain, not sure there is a solution.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/1ylRoHQPerIJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Nick R

unread,
Apr 2, 2012, 8:59:45 PM4/2/12
to ang...@googlegroups.com
Uh oh.  The null promises problem also affects directives, which do not re-evaluate.

I gave one of my directives an expression that evaluates to a promise, and scope.$eval just gave me undefined.

Fortunately I can watch the expression until it isn't undefined anymore.
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

Misko Hevery

unread,
Apr 3, 2012, 12:30:42 PM4/3/12
to ang...@googlegroups.com
what would you expect to get out of $eval when you pass in unresolved promise?

To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/IxomUg31eiQJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages