0

I use a function in the for loop. I can not export the result of the function.

I want to export the result to the data array. How can I do it ?

function opPerformance(callback) {
    var data = [];
    dbConn.connect(function(err) {
        var request = new sql.Request(dbConn);
        request.query('select * from Kullanici ORDER BY Kullanici_Adi ASC ').then(function(recordset) {
            var results = [];
            for (var i in recordset.recordset) {
                data[i] = {
                    kullanici_adi:recordset.recordset[i].Kullanici_Adi,
                    performance:'0'
                };

                opPerformanceData(recordset.recordset[i].Kullanici_Adi, function ( result ){
                    data[i]['performance'] = result; // This is not working, I can not get the end out.
                    console.log(result); // This works.
                });
            }
            callback(data);
        });
    });
};
Sven
  • 5,155
  • 29
  • 53
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – baao Apr 23 '18 at 22:18
  • 1
    in the function opPerformanceData you pass a callback that will get executed asynchronously, and therefore when you call callback(data), data will be empty – DZDomi Apr 23 '18 at 22:19

0 Answers0