如何获得异步函数的返回值?

时间:2017-03-23 04:38:24

标签: angularjs arrays asynchronous

我希望在i console 该函数的名称时将异步函数的返回值作为数组。我之前得到的是一个物体。

1)我在控制器中写的内容:

f
      $$state: Object
        status: 1
        value: Array[33]
          0: "ALAIA"
          1: "ALBERTA FERRETTI"
          2: "Alessandra Rich"
          3: "Alexander Mcqueen"
          4: "Alexander Wang"
          5: "Alice + Olivia"
          6: "Amanda Wakeley"
          7: "Balenciaga"
          8: "Bcbg Maxazria"
          9: "Bcbg Maz Azria"
          10: "Biyan"
          11: "Burberry"
          12: "Calvin Klein"
          13: "Casadei"
          14: "Chanel"
          15: "Charlotte Olympia"
          16: "Chloe"
          17: "Christian Dior"
          18: "Dolce & Gabbana"
          19: "Emilia Wickstead"
          20: "Farah Khan"
          21: "Fendi"
          22: "Gucci"
          23: "Halston Heritage"
          24: "Herve Leger"
          25: "Lanvin"
          26: "Louis Leeman"
          27: "Mary Katrantzou"
          28: "Roberto Cavalli"
          29: "Sebastian Gunawan"
          30: "Self-Portrait"
          31: "Valentino"
          32: "Versace"
         length: 
        __proto__: Array[0]
       __proto__: Object
      __proto__: Object

2)console.log的结果($ scope.allBrands()):

["ALAIA", "ALBERTA FERRETTI", "Alessandra Rich", "Alexander Mcqueen", "Alexander Wang", "Alice + Olivia", "Amanda Wakeley", "Balenciaga", "Bcbg Maxazria", "Bcbg Maz Azria", "Biyan", "Burberry", "Calvin Klein", "Casadei", "Chanel", "Charlotte Olympia", "Chloe", "Christian Dior", "Dolce & Gabbana", "Emilia Wickstead", "Farah Khan", "Fendi", "Gucci", "Halston Heritage", "Herve Leger", "Lanvin", "Louis Leeman", "Mary Katrantzou", "Roberto Cavalli", "Sebastian Gunawan", "Self-Portrait", "Valentino", "Versace"]

3)我希望实现的结果:

$scope.allBrands = function(){    
   var brands = [];
   return BrandService.get().then(function(data) {      
       angular.forEach(data, function(value, key) {
           brands.push(value.name);
       });
       return brands;
   });
};

var promise = $scope.allBrands(); 

promise.then(function(result){
    console.log(result);
},
function(error){
    console.log(error);
});

有人能指出我应该怎么做?

<小时/> *编辑

这是解决方案

 user.signUpInBackground {(success: Bool, error: Error?) in
                if success {
                    print("signed up")
                    AVUser.logInWithUsername(inBackground: user.username!, password: user.password!, block: {(user: AVUser?, error: Error?) in
                        if let user = user {
                            UserDefaults.standard.set(user.username, forKey: "username")
                            UserDefaults.standard.synchronize()
                            let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
                            appDelegate.login()
                        }
                    })
                }else {
                    print(error!.localizedDescription)
                }
            }

2 个答案:

答案 0 :(得分:2)

异步调用不按您的思维方式工作(同步方式)。一旦您调用它们,它们就不会返回值。您必须等到服务器响应请求。

在这里,你console.log allBrands函数返回promise对象(似乎$resource承诺),这就是安慰了。并且您可以在promise对象到达时立即看到您的响应。我会说你应该使用.then覆盖你的promise对象,你可以从API返回一个数据。

$scope.allBrands().then(successCB(states){
   $scope.states = states;
   console.log(states);
}, errorCB(error){
   console.log(error)
}) 

答案 1 :(得分:0)

你不应该返回promise,而是可以在resolve函数中返回一个新数组:

$scope.allBrands = function(){
  BrandService.get().then(function(data) {
       return data.map(function(item) { return item.name })
  });
};