I have a callback function and within that, a for loop containing another calllback.
I am struggling with understanding the variable scope. I need to access the out
variable throughout the nested structure:
var out = {"foo":123};
persistence.getAllApiKeys(function(err, allKeys){
for (var prop in allKeys) {
out = {"baz":456};
persistence.getApiKeyValue("test", function(err2, value) {
out = {"success":true}; // <--does not update
});
}
console.log(out);
})
Outputs:
{ baz: 456 }
How can I access the out variable inside the final callback? i.e. set it to "success":true
?