我正在使用viewer.getProperties(dbId, onSuccessCallback, onErrorCallback)方法來獲取查看器中對象的屬性。我想爲所有選定對象運行該方法,爲每個對象提取屬性的子集,並將子集呈現在表中。爲多個元素並行運行viewer.getProperties,然後處理結果
var subsets = [];
var selectFunctions = [];
handleSelection(selection, addProps, onError);
function handleSelection(selection, onSuccess, onError) {
for (var i = 0; i < selection.length; i++)
selectFunctions.push(_viewer.getProperties(selection[i], onSuccess, onError));
}
function addProps(data) {
var props = [];
for (var prop in data.properties) {
//Add property to props if some condition is true...
}
subsets.push(props);
}
Promise.all(_selectFunctions).then(function() {
console.log("Handled all selections");
//Add subsets to table...
}).catch(function (error) {
console.log("ERRROR");
});
由於getProperties異步運行,我無法在更新表之前等待所有對象。該表每次更新一個對象,我們寧願一次更新所有對象。阻止IO不是問題。
正如我能看到的,我一直在尋找來自bluebird.js的Promise.all()以控制執行並等待所有getProperties調用返回,但到目前爲止失敗。
問候, Torjus
您能否接受答覆作爲答覆或解釋它如何解決您的問題?謝謝 –
謝謝Philippe!這工作完美,我認爲getProperties方法有問題,但事實證明我誤解了解析和拒絕的使用。 – torjuss