I'm writing a program that basically does the below tasks.
- Read a json file.
- Insert the item from the file to DynamoDB
Below is my code.
const AWS = require('aws-sdk');
var fs = require('fs');
var docClient = new AWS.DynamoDB.DocumentClient();
var i = 0;
var allMovies = JSON.parse(fs.readFileSync('JsonFiles/Cars.json', 'utf8'));
allMovies.forEach(function (user) {
var params = {
TableName: "CarsData",
Item: {
"id": user.id,
"make": user.Make.toLowerCase(),
"model": user.Model.toLowerCase(),
"year": user.Year,
}
};
if (i % 100 == 0) {
setTimeout(function () {
console.log('Blah blah blah blah extra-blah');
}, 3000);
}
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add user", user.Region, ". Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log(`PutItem succeeded: `, i);
}
});
i++;
});
Basically, I'm trying to, run the program and increment i by 1. Once i reaches 100, I want the entire program to pause for 3 seconds and then continue till next time 100 records and so on... the output should be like
put item succeeded: 3825
...99 more records....
Blah blah blah blah extra-blah
----------wait for 3 seconds-----
put item succeeded: 3825
...99 more records....
Blah blah blah blah extra-blah
----------wait for 3 seconds-----
.........and so on till the entire data is inserted
When I run the above code the output that I get is as below.
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
Blah blah blah blah extra-blah
PutItem succeeded: 3825
PutItem succeeded: 3825
This is quite confusing, please let me know where am I going wrong and how can I fix this.
Thanks