I have an array (targets
) in global scope, the values of which I am passing to an external function [third party library, externalConverter
] that does some text conversion. The values of this array are being passed in to the convert
function and the conversion is happening fine.
const targets = [‘box’, ’box1’, ’box2’, ’box3’]
for (var i = 0; i < targets.length; ++i) {
console.log(targets[i]); // this is coming out fine
externalConverter
.convert(data.text, targets[I])
.then(results => {
data.convertedText.push({
[targets[i]]: results[0]
});
//above convertedText array comes out as
//{“undefined: ”, “nice converted text”}, ...
})
}
Inside the result of the Promise, I am trying to access the targets
values but getting undefined
values inside the function above. I am not sure why targets is suddenly becoming undefined
Any ideas?