Use whilst instead of forever

this gets rid of the Error object
This commit is contained in:
Girish Ramakrishnan
2019-12-04 11:17:42 -08:00
parent 30eccfb54b
commit 85e17b570b
3 changed files with 24 additions and 26 deletions

View File

@@ -161,29 +161,27 @@ function listDir(apiConfig, dir, batchSize, iteratorCallback, callback) {
MaxKeys: batchSize
};
async.forever(function listAndDownload(foreverCallback) {
s3.listObjects(listParams, function (error, listData) {
if (error) return foreverCallback(error);
let done = false;
if (listData.Contents.length === 0) return foreverCallback(new Error('Done'));
async.whilst(() => !done, function listAndDownload(whilstCallback) {
s3.listObjects(listParams, function (error, listData) {
if (error) return whilstCallback(error);
if (listData.Contents.length === 0) { done = true; return whilstCallback(); }
const entries = listData.Contents.map(function (c) { return { fullPath: c.Key, size: c.Size }; });
iteratorCallback(entries, function (error) {
if (error) return foreverCallback(error);
if (error) return whilstCallback(error);
if (!listData.IsTruncated) return foreverCallback(new Error('Done'));
if (!listData.IsTruncated) { done = true; return whilstCallback(); }
listParams.Marker = listData.Contents[listData.Contents.length - 1].Key; // NextMarker is returned only with delimiter
foreverCallback();
whilstCallback();
});
});
}, function (error) {
if (error.message === 'Done') return callback(null);
callback(error);
});
}, callback);
});
}