0

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);
}
Ashish
  • 6,791
  • 3
  • 26
  • 48
Hoz.Coder
  • 13
  • 4

1 Answers1

0

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);
}
Ali Demirci
  • 5,302
  • 7
  • 39
  • 66