Convert aws api calls that require continuation tokens into simple iterators.
const iterify = ({ client, method, params, hint, token }) => {
let finished = false
const values = []
return {
[Symbol.asyncIterator] () {
return this
},
next: async () => {
if (values.length) {
return { value: { data: values.shift() } }
}
if (finished) {
return { done: true }
}
let res = null
try {
res = await client[method](params).promise()
} catch (err) {
return { value: { err } }
}
if (res.Contents && res.Contents.length) {
values.push(...res.Contents)
}
if (res[hint]) {
params[token] = res[token]
} else {
finished = true
}
const data = values.shift()
if (typeof data === 'undefined') {
return { done: true }
}
return { value: { data } }
}
}
}
const itr = itrify({
client: require('aws-sdk/clients/s3'),
method: 'listObjectsV2',
hint: 'NextContinuationToken',
token: 'ContinuationToken'
})
for await (const { err, data } of itr) {
if (err) ...
console.log(data)
}