It may be a silly question, but couldn't find the proper code sample with best approach in JavaScript. I have 1000's JSON of object and want to send 100 by 100. After getting success as response i want to send next 100. Thanks in advance
Actually is quite simple if you're considering using async/await
. The concept is to send your json Synchronously
.
Haven't test the code but, more or less it should be like this.
const x = [{...}] // assuming that x is your array of object
const sendByChunks = async (chunkSize) => {
return new Promise((resolve, reject) => {
let start = 0
let end = 0
while (start <= x.length) {
try {
end = (end + chunkSize) > x.length ? x.length : (end + chunkSize) // set the end index
const payload = x.slice(start, end) // slice the array
start = end
await postBatchData(payload) // send to your API
} catch (error) {
reject(`Error postBatchData from ${start} to ${end}`) // log something
}
resolve('Success')
}
})
}
sendByChunks(100)
.then(response => { // handle response (your resolve) })
.catch(error => { // handle error (your reject) })