What is the scope of JS variables in anonymous functions
        Posted  
        
            by 
                smorhaim
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by smorhaim
        
        
        
        Published on 2012-11-01T22:57:31Z
        Indexed on 
            2012/11/01
            23:00 UTC
        
        
        Read the original article
        Hit count: 378
        
JavaScript
|node.js
Why does this code returns $products empty? If I test for $products inside the function it does show data... but once it finishes I can't seem to get the data.
var $products = new Array();
connection.query($sql, function(err, rows, fields) {
    if (err) throw err;
    for(i=0; i< rows.length; i++)
    {
        $products[rows[i].source_identifier] = "xyz";
    }
});
connection.end();
console.log($products); // Shows empty.
        © Stack Overflow or respective owner