title: BatchWrite in AWS dynamo db skipping some items
body: <p>I am trying to write Items to AWS dynamo db using node SDK. The problem I am facing is that when I write batch items to AWS in <strong>parallel</strong> using threads, some of the items are not written to database. The number of items are written are random. For instance, If I run my code 3 times, at one time it would be 150, next it would 200 and third time it could be 135. In addition, when I write the items sequentially without threads, even then some of the items are not written.However, in this case the items are less missing. For instance if the total number of items is 300 then the items written are 298.  I investigated the problem to see if there any unprocessed items but the batchWrite method returns nothing. It means that all the items are being processed correctly. Please note that I have <strong><em>OnDemand provision</em></strong> for my respective database so I do not expect any throttling issues. So here is my code.</p>
  
  <pre><code> exports.run = async function() {
  
    **This is the function which runs first !!!!!**
  
    const data = await getArrayOfObjects();
    console.log("TOTAL PRICE CHANGES")  
    console.log(data.length)
    const batchesOfData = makeBatches(data)
    const threads = new Set();
    console.log("**********")
    console.log(batchesOfData.length)
    console.log("**********")
    for(let i = 0; i &lt; batchesOfData.length; i++) {
      console.log("BATCH!!!!!")
      console.log(i)
      console.log(batchesOfData[i].length)  
      // Sequential Approach
      const response = await compensationHelper.createItems(batchesOfData[i])
      console.log("RESPONSE")
      console.log(response)
  
      Parallel approach
      // const workerResult = await runService(batchesOfData[i])
      // console.log("WORKER RESUULT!!!!")
      // console.log(workerResult);
  
    }
  }
  
  exports.updateItemsInBatch = async function(data, tableName) {
    console.log("WRITING DATA")
    console.log(data.length)
    const batchItems = {
      RequestItems: {},
    };
  
    batchItems.RequestItems[tableName] = data;
    try {
      const result = await documentClient.batchWrite(batchItems).promise();
      console.log("UNPROCESSED ITEMS")
      console.log(result)
      if (result instanceof Error) {
        console.log(`[Error]: ${JSON.stringify(Error)}`);
        throw new Error(result);
      }
      return Promise.resolve(true);
    } catch (err) {
      console.error(`[Error]: ${JSON.stringify(err.message)}`);
      return Promise.reject(new Error(err));
    }
  };
  
  exports.convertToAWSCompatibleFormat = function(data) {
    const awsCompatibleData = [];
    data.forEach(record =&gt; awsCompatibleData.push({ PutRequest: { Item: record } }));
    return awsCompatibleData;
  };
  
  const createItems = async function(itemList) {
    try {
      const objectsList = [];
      for (let index = 0; index &lt; itemList.length; index++) {
        try {
          const itemListObj = itemList[index];
          const ObjToBeInserted = {
            // some data assignments here
          };
  
          objectsList.push(ObjToBeInserted);
          if (
            objectsList.length &gt;= AWS_BATCH_SIZE ||
            index === itemList.length - 1
          ) {
              const awsCompatiableFormat = convertToAWSCompatibleFormat(
                objectsList
              );
              await updateItemsInBatch(
                awsCompatiableFormat,
                process.env.myTableName
              );
          }
        } catch (error) {
          console.log(`[Error]: ${JSON.stringify(error)}`);
        }
      }
  
      return Promise.resolve(true);
    } catch (err) {
      return Promise.reject(new Error(err));
    }
  };
  
  const makeBatches = products =&gt; {
    const productBatches = [];
    let countr = -1;
    for (let index = 0; index &lt; products.length; index++) {
      if (index % AWS_BATCH_SIZE === 0) {
        countr++;
        productBatches[countr] = [];
        if (countr === MAX_BATCHES) {
          break;
        }
      }
      try {
        productBatches[countr].push(products[index]);
      } catch (error) {
        continue;
      }
    }
    return productBatches;
  };
  
  async function runService(workerData) {
    return new Promise((resolve, reject) =&gt; {
      const worker = new Worker(path.join(__dirname, './worker.js'), { workerData });
      worker.on('message', resolve);
      worker.on('error', reject);
      worker.on('exit', (code) =&gt; {
        if (code !== 0)
          reject(new Error(`Worker stopped with exit code ${code}`));
      })
    })
  }
  
  // My worker file
  'use strict';
  
  const { workerData, parentPort } = require('worker_threads')
  const creatItems = require('myscripts')
  // You can do any heavy stuff here, in a synchronous way
  // without blocking the "main thread"
  console.log("I AM A NEW THREAD")
  createItems(workerData)
  // console.log('Going to write tons of content on file '+workerData);
  parentPort.postMessage({ fileName: workerData, status: 'Done' })
  </code></pre>