Search code examples
javascriptjsones6-promise

Batch large JSON data set in JavaScript and post it to server


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


Solution

  • Actually is quite simple if you're considering using async/await. The concept is to send your json Synchronously.

    1. Split your payload
    2. Synchronously await until your server gave it response
    3. Handle the response (proceed reject if error occurs, this will break the loop)
    4. Proceed change the index

    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) })