I'm trying to wrap my head around promises and struggling a little bit.
This is the code:
// For Every row.
var prom = new Promise( (resolve, reject) => {
existing_rows.forEach ((item,index) => {
console.log("onto index: " + index);
//We just need to set item[2]!
if (item[1].indexOf('%')===-1) {
//Cool no percentage sign.
translateClient.translate(item[1], 'th')
.then((results) => {
const translation = results[0];
console.log("translation is: " + translation);
item[2] = translation;
});
}
})
resolve("Stuff worked!");
})
prom.then(
(result) => {
//Cool that's finished.
console.log("done!")
console.log(JSON.stringify(existing_rows));
}
)
existing_rows is just an array I'm looking at with some stuff to translate using the google api. I want the translations to all be placed in the existing_rows array before I log it in the prom.then block.
At the moment, the output is in this order:
onto index 0
onto index ..
onto index 8
done!
logs the array without the translations
translation is: ...... x 8
Thanks