2016-05-30 26 views
0

AngularJS我们的工作很多有承诺,我想知道是否有某种方式来手动手动设置承诺的结果(无论是成功与否),而不rejectresolveAngularJS - 设定承诺手动

喜欢的东西

var list = []; 

function call(){ 
    async() 
     .then(function(response){ 
      console.info('yay'); 
     }) 
     .catch(function(error){ 
      console.info('nay'); 
     }); 
} 

function async(){ 
    var item = { 
     id: 1, 
     defer: $q.defer() 
    }; 

    list.push(item); 

    sendRequestToAsyncService(item.id); 

    return item.defer.promise; 
} 

function receiveDataFromAsyncService(data, id){ 
    for(var i = 0; i < list.length; i++){ 
     if(id === list[i].id){ 
      list[i].defer.promise = data; 
     }  
    } 
} 
+1

如果您不打算使用手动方法'resolve'和'reject',手动设置是什么意思?你试图解决的更高层次的问题是什么? – charlietfl

回答

0

摆弄了一会我找到了答案后,我不得不使用resolve /在延迟根级别拒绝功能。

list[i].defer.resolve(response);