I have an api function that converts an image file to a src url. And this one works well.
const requestImageUrl = (data) => {
return axios.post(baseUrl + `/image`, data, {
headers: {
"Content-Type": "multipart/form-data",
},
});
};
I want to convert all images to urls and store them in a array.
So I made a code like down below. But it didn't work really well.
convertToUrl(images) {
let imageUrls = []
for (let i=0; i<images.length; i++) {
const formData = new FormData()
formData.append('file', images[i])
PostingApi.requestImageUrl(formData).then(res => {
imageUrls.push(res.data.data.image)
})
}
return imageUrls
},
So I changed code like this using async and await. And it worked well.
async convertToUrl(images) {
let imageUrls = []
for (let i=0; i<images.length; i++) {
const formData = new FormData()
formData.append('file', images[i])
const res = await PostingApi.requestImageUrl(formData)
imageUrls.push(res.data.data.image)
}
return imageUrls
},
Both returned same values, array which has length of 2. But they were slightly different as you can see in the picture down below. When I open up the arrays in console, they had same things, but when they are enclosed, it wasn't same. Could you tell me why they return diffrent values?
[First one is the one that didn't use async, Second one is the one used async][1]
[1]: https://i.stack.imgur.com/fMhdx.png