title: Using async/await within a read stream to batch upload to DynamoDB
body: <p>A bit of a Node novice here...</p>
  
  <p>I'm trying to write a function that pulls a CSV down from S3 and batch-writes the items to DynamoDB. DynamoDB has a limit of 25 in each batch so I need write the entries as I go. The problem I'm running into is that my <code>await</code> function to execute the DB write only fires at the <code>.end()</code>, rather than when I check.</p>
  
  <p>I understand that I can't execute things like this, but I'm not sure how to fix it? I'm using Node12.</p>
  
  <p>Thanks.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>async function populateTable(
    dataFile: bucketKey,
    tableName: string
  ): Promise&lt;void&gt; {
    const s3 = getS3Client();
    const stream = s3.getObject(dataFile).createReadStream();
  
    const BATCH_COUNT = 25; // Max size to write to DynamoDB
    let counter = 0;
    let datarows: any = [];
    let datarow = {};
  
    stream
      .pipe(parse(DATA_HEADERS))
      .on("data", async function(data: DataRow) {
        counter++;
  
        datarow = {
          PutRequest: {
            Item: data
          }
        };
  
        datarows.push(datarow);
  
        if (counter % BATCH_COUNT === 0) {
          console.log("before batch write " + counter); // This fires!
          await batchWriteToDynamo(datarows, tableName); // I want this function to fully execute before moving on
          console.log("after batch write " + counter); // This does not
          datarows = [];
        }
      })
      .on("end", async function() {
        await batchWriteToDynamo(datarows, tableName); // This fires!
      });
  }</code></pre>
  </div>
  </div>
  </p>