This Lambda function does an http-request and then writes the data into DynamoDB. I'm wondering why the function is set asynchronously. Wouldn't it be the same if it weren't async? As far as I know, await tells the code to pause and to continue after httprequest() is done. Isn't that the same like running the code synchronously?
exports.handler = async (event) => {
try {
const data = await httprequest();
for (var i = 0; i < data.d.results.length; i++){
var iden = Date.now();
var identifier = iden.toString();
datestring.substring(4,6) + "-" + datestring.substring(6,8);
//add to dynamodb
var params = {
Item: {
id: identifier,
date: data.d.results[i].DAY_T,
},
TableName: 'DB'
};
await docClient.put(params).promise();
}
console.log('Document inserted.');
return JSON.stringify(data);
} catch(err) {
console.log(err);
return err;
}
};
await docClient. --- To use await the function it is in needs to be async.awaitgives the JavaScript engine chance to run some other bit of JS rather than block waiting a response on the called function, just like promises.