Currently working on a JavaScript project that requires the use of await/async to fetch data from an API.
Here is a snippet of my code:
const fetchData = async () => {
let data = [];
for (let i = 0; i < this.data.length; i++) {
const formData = new FormData();
const fetchLogs = () => {
getData.request(new URLSearchParams(formData), (response) => {
if (response.status >= 200 && response.status < 300) {
console.log('Data response:', response);
return response.data.data;
}
});
};
let logsData = await fetchLogs();
console.warn(logsData);
data.concat(logsData);
}
return data;
};
However, the issue I'm facing is that the logs are being returned before the response is received.
myBabelConfig.js
module.exports = {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: 3.6,
modules: false,
},
],
],
};
"vue": "^2.6.0"
Can you spot what I might be doing wrong in this setup?