Skip to content Skip to sidebar Skip to footer

How Do You Call An Outer Functions Return After An Inner Async Function Finishes Running?

This is my code: .filter('getUserName', function(User) { return function(id) { User.get({ _id: id }, function(user) { return user.name; }); }; });

Solution 1:

This is a terrible fit for a filter, but just as an intellectual exercise, you could have the filter return some default behavior (i.e. return blank) until data is fetched, and once fetched apply the filter. This would necessitate the filter to be $stateful, which is very wasteful - it will run on every digest cycle.

app.filter("foo", function($timeout){
  var cache = {};

  functiongenFoo(input){
    $timeout(function(){
      cache[input] = input + "foo!";
    }, 1000);
  }

  var filter = function(input){
    if (inputin cache) return cache[input];
    genFoo(input);
    return"";
  };

  filter.$stateful = true;
  return filter;

});

Plunker

DO NOT do this as a filter :)

Post a Comment for "How Do You Call An Outer Functions Return After An Inner Async Function Finishes Running?"