Fonctions asynchrones dans une boucle
Bonjour à tous,
Je block sur un truc et fais donc appel à nos éminents habitants du forum :
Je voudrais créer de manière asynchrone un array à partir d'un string :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
arrMetamorph = function(str) {
var newArr = {};
arr = str.split(" ");
arr.map(function(el){
getTransformation(el, function(output){
if(output){
if (typeof(newArr[output])=="undefined") {
newArr[output]=1;
}else{
newArr[output]+=1
}
}
});
});
console.log(JSON.stringify(newArr));
}
getTransformation = function(input,cb){
setTimeout(function () {
cb('"'+input+"'");
}, 2000);
}
arrMetamorph('1 2 3 4 5') |
Normalement getTransformation tape dans une bdd
Le résultat est {}. Quelqu'un pourrait-il me donner une astuce pour lancer
Code:
1 2
|
console.log(JSON.stringify(newArr)); |
un fois la boucle terminée ?