title: How to use promises with multiple calls to dynamodb scan
body: <p>I receive a promise from dynamodb.scan(). When the promise is resolved if the response includes LastEvaluatedKey then dynamodb.scan() must be called again. When the response does not include the LastEvaluatedKey object all records of the table have been scanned and the query complete. I do not know the number of calls to scan() needed in advance. I don't know how to code this. This is what I have now:</p>
  
  <pre><code>function scan(options, startKey) {
    var parms = {
      TableName: options.source,
      ProjectionExpression: "book, bid, #kee, #unt, pid, #txt",
      FilterExpression: "contains(#txt, :v_qs)",
      ExpressionAttributeNames: {
        "#unt": "unit",
        "#txt": "text",
        "#kee": "key"
      },
      ExpressionAttributeValues: {
        ":v_qs": options.queryTransformed
      }
    };
  
    if (startKey) {
       parms.ExclusiveStartKey = startKey;
    }
  
    return dynamoDb.scan(parms).promise();
  }
  
  scan(options).then(function(response) {
    if (response.LastEvaluatedKey) {
      searchResults.push(response);
      return scan(options, response.LastEvaluatedkey);
    }
    else {
      return response
    }
  }).then(function(response) {
    if (response.LastEvaluatedKey) {
      searchResults.push(response);
      return scan(options, response.LastEvaluatedKey);
    }
    else {
      return response;
    }
  }).then(function(response) {
    //this is crazy - bailing even if another call is needed
    searchResults.push(response);
    return generateSearchResponse();
  });
  </code></pre>
  
  <p>This clearly is not the way to do it. This code is part of a AWS Lambda node function. Thanks in advance for your help. </p>