I have an array of substrings and a string. I am using a loop of substrings and trying to replace the substring array value. If I console inside after replacing, it works as expected, but when trying to console outside, it gives me the original string.
const matches = text.match(regex);
let response = "";
if (matches) {
let newstr = text;
for (const tlfUri of matches) {
response = fetchData(tlfUri).then((str) => {
if (str != "") {
text = text.split(tlfUri).join(str);
}
console.log(text); //getting replaced text inside .then
});
}
}
console.log(text); //getting actual text, but want replaced text