I want to push all volume to arrayVolume but can't. In my opinion the problem is asynchronous. But I search, read document but can't do it run correct. Please help me solve the code if you can. That is my gratitude to you. Many thanks!
function kl(data){
var arrayVolume = [];
for(var key in data){
var dbId = data[key];
viewer.getProperties(dbId, function(e){
var propertiesObj = e.properties;
propertiesObj.forEach(myF);
function myF(obj){
if(obj.displayName === "Volume"){
var volume = obj.displayValue;
arrayVolume.push(volume);
}
}
});
}
alert(arrayVolume);
}
If that viewer.getProperties
is an async operation, like an http call, you should use promises.
For example:
async function kl(data) {
var arrayVolume = [];
for (var key in data) {
var dbId = data[key];
await new Promise((resolve, reject) => {
viewer.getProperties(dbId, (e) => {
var propertiesObj = e.properties;
propertiesObj.forEach(myF);
function myF(obj) {
if (obj.displayName === "Volume") {
var volume = obj.displayValue;
arrayVolume.push(volume);
}
}
return resolve();
});
});
}
alert(arrayVolume);
}
I have the same problem again here. How can I deal with it. Actually I still can't understand async await promises. klht return no object. @Ali Demirci
var klht = [];
dataViewerFirst.forEach(obj => {
var externalId = obj.externalId;
var _projectName = obj.projectName;
var status = obj.status;
if(externalId !== null && externalId !== undefined && _projectName === projectName && status === 1){
viewer.model.getExternalIdMapping(data => first(data));
async function first(data){
dbId = data[externalId];
await new Promise((resolve, reject) => {
viewer.getProperties(dbId, function(e){
var propertiesObj = e.properties;
propertiesObj.forEach(myF);
function myF(obj){
if(obj.displayName === "Volume"){
var volume = obj.displayValue;
klht.push(volume);
console.log('vol '+volume);
}
}
return resolve();
});
})
//return resolve();
}
}
})
console.log('v '+klht);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.