2017-02-14 97 views
0

我想弄清楚什麼數據結構的承諾,我可以在$q.all()函數中使用。我知道我可以給它一組承諾和承諾地圖,但是我可以給它一個任意的深度圖嗎?例如,我可以這樣做:

var map = {}; 
map["A"] = {}; 
map["A"]["B"] = MyService.getPromise(); 

$q.all(map).then(function(resultMap){ 
    // does resultMap match the structure of the input map? 
    console.debug(resultMap["A"]["B"]) 
}) 

如果我不能,有什麼建議的方式做這樣的事情?我想知道我的結果數據地圖中的原始水平。

+0

不,你不能傳遞一個任意對象給它。你試圖解決什麼更高水平的問題? – charlietfl

回答

0

對我來說,這看起來非常簡單,工作原理:

var promises = []; 

promises.push(MyService.getPromise().then(function(result) { 
    map["A"]["B"] = result; 
})); 

promises.push(MyService.getPromise().then(function(result) { 
    map["A"]["C"] = result; 
})); 

... 

$q.all(promises).then(function() { 
    // map is initialized here 
}) 
+0

你怎麼知道結果是C或B? – rex