Skip to content

Instantly share code, notes, and snippets.

@aaronshaf
Last active May 13, 2022 11:00
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aaronshaf/cff7246a6dbe3ae8e12b9c8155d17362 to your computer and use it in GitHub Desktop.
Save aaronshaf/cff7246a6dbe3ae8e12b9c8155d17362 to your computer and use it in GitHub Desktop.
Use async generators and async iterators with DynamoDB's scan
const { docClient } = require('../services/dynamodb')
exports.findAllItems = async function* () {
let response = {}
let ExclusiveStartKey
do {
response = await docClient.scan({
TableName: 'mytable',
Limit: 500,
ExclusiveStartKey
}).promise()
const items = response.Items
for (let item of items) {
yield item
}
ExclusiveStartKey = response.LastEvaluatedKey
} while (response.LastEvaluatedKey)
}
const findAllItems = require('./generator.js')
for await (const item of findAllItems()) {
console.log(item)
}
@trilom
Copy link

trilom commented May 8, 2019

this code is so hip, have a star.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment