This question already has answers here:
How do I return the response from an asynchronous call?
                                
                                    (38个答案)
                                
                        
                4年前关闭。
            
        

我有一个函数func,它解析一个promise,然后尝试返回局部变量returnval的更新值。

func: function(){
   returnval = false;
   var promise = new Ember.RSVP.Promise(function(resolve) {
       // some stuff ..
       returnval = true;
       resolve();
   });
   promise.then(function(){
       return returnval;
   });
}


我想return returnval从promise对象返回。如何从returnval返回func的更新值?

最佳答案

要使函数返回:

func: function(){
   returnval = false;
   var promise = new Ember.RSVP.Promise(function(resolve) {
       // some stuff ..
       returnval = true;
       resolve();
   });
   promise.then(function(){
       return returnval;
   });
   return returnval; //this will return from the function call
}

关于javascript - javascript promises return ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31962261/

10-13 00:38