How do you call an outer functions return after an inner async function finishes running?

后端 未结 1 1853
耶瑟儿~
耶瑟儿~ 2021-01-22 11:32

This is my code:

.filter(\'getUserName\', function(User) {
    return function(id) {
       User.get({ _id: id }, function(user) {
           return user.name;
          


        
相关标签:
1条回答
  • 2021-01-22 12:22

    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 = {};
    
      function genFoo(input){
        $timeout(function(){
          cache[input] = input + "foo!";
        }, 1000);
      }
    
      var filter = function(input){
        if (input in cache) return cache[input];
        genFoo(input);
        return "";
      };
    
      filter.$stateful = true;
      return filter;
    
    });
    

    Plunker

    DO NOT do this as a filter :)

    0 讨论(0)
提交回复
热议问题