I'm going to mock your WorkFlow.Utils.GetTextParameterSelected(id, currency)
method basically by returning a promise that resolves after 1 second with the id, description and currency:
var descripciones = [
'hola',
'soy',
'la descripción'
];
function getDescripcion(id, currency) {
return new Promise((resolve, reject) => {
window.setTimeout(() => {
resolve({
id: id,
descripcion: descripciones[id],
moneda: currency
});
}, 1000);
});
}
In your loop $.each
you are inserting promises to an array called, very imaginatively promesas
. Finished the loop, you call
Promise.all(promesas).then((values) => {
console.log(values);
});
To obtain the collection of values obtained once all the promises have been resolved. The order in which they appear is the same in which they were inserted, regardless of the order in which they were resolved.
var descripciones = [
'hola',
'soy',
'la descripción'
];
var jsonTransfers = [{
currencyType: 'USD'
},
{
currencyType: 'EUR'
},
{
currencyType: 'CLP'
}
];
function getDescripcion(id, currency) {
return new Promise((resolve, reject) => {
window.setTimeout(() => {
resolve({
id: id,
descripcion: descripciones[id],
moneda: currency
});
}, 1000);
});
}
var promesas = [];
$.each(jsonTransfers, function(index, value) {
promesas.push(getDescripcion(index, value.currencyType));
});
Promise.all(promesas).then((respuestas) => {
console.log(respuestas);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>