I'm new in JS and I don't know hot to achieve this. I have a JS object like this:
{ 'U9B6QVB9B': 26, 'U2B2HKB5H': 23, 'U4Q7S4JTB': 41 }
I want to apply a async function from Slack API to each key (which represent the ID of a Slack user), in order to obtain the user's name. The first try was something like this:
var userIdToName = function(cuenta){
var cuentaF = {};
for (var key in cuenta){
slack.users.info({token, key}, function(err, data){
var nombre = data.user.name;
cuentaF[nombre] = cuenta[key];
})
}
return cuentaF;
}
But now, I know that it isn't possible because 'var key' changes its value and the loop ends before the first async call ends. I have been reading about Promises and other JS and Node tools, but I until the moment I haven't been able to imagine how to use them to achieve it.
Thanks in advance.
Note: I'm using NodeJS v4.6.0, but I could change it to another version if it would be necessary.