I'm newbie in JS and Vue.
I have 100 rows of url that sent to Backend. I used a for loop to call api with data is each row.
<template>
<table>
<thead>
<tr>
<th>No.</th>
<th>Url</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr v-for="(post, index) in posts">
<td>{{ index + 1}}</td>
<td>{{ post.url}}</td>
<td class="classStatus(post.status)">{{ post.status}}</td>
</tr>
</tbody>
</table>
Script:
const apiUrl = 'example.com/api/createPost'
for (post of this.posts) {
// Sent only posts with status READY or FAIL
if (post.status === STATUS_SUCCESS) {
continue;
}
// Call api to process post one by one
this.apiService(apiUrl, post)
.then(result => {
post.status = STATUS_SUCCESS
}).catch(err => {
post.status = STATUS_FAIL
})
}
It's work with cases single post (api call 1 time):
- 1 valid url (inserted in backend) -> post.status updated to STATUS_SUCCESS
- 1 invalid url (Throw exception in backend console) -> post.status updated to STATUS_FAIL
But, in cases multiple posts:
- row invalid url before valid => both rows post.status updated to FAIL
- row valid url before invalid => status updated correctly, but I check network call API just 1 time.
Can you help me to know why there are multiple posts happend?
- I dont know why they both fail status if a invalid data called before the valid
- Is my script was an Anti pattern? (call API inside loop)