0

I am working on a basic function inside of AWS Lambda, running on NodeJS. At the portion of the code I've tagged, the development environment is giving me an error saying Parsing error: Unexpected token tieDown()

If I remove the await portion the error disappears, however it is not possible to remove this because I obviously need to WAIT for that function to finish and return an object before it can begin breaking it down.

As you can see the main handler is already async, and the first await/promise portion works perfectly. So why is this failing?

const AWS = require('aws-sdk');
  const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
  exports.handler = async (event, context) => {
    const rDom = event.referring_domain;
    const sIP = event.source_ip;
    const uA = event.user_agent;
    
    // THIS AWAIT WORKS FINE
    await checkDomain().then(data => {
     if(isEmpty(data)){
      let _response = {
        statusCode: 403,
        body: "Permission denied"
       };
       console.log("Perm denied");
      return _response;
     }else{
        // ERROR HERE!!!
        await tieDown().then(data =>{
         
        });
     };
     
     }).catch((err) => {
     console.error(err);
    });
    
  };
  
 function checkDomain(rDom){
  const params = {
   
   Key : {
     "domain" : "sample.com"
   },
   TableName: "myTable"
  };
  return ddb.get(params, function(err, data){
   if (err) console.log(err.stack);
  }).promise();
 }
 
 function isEmpty(obj) {
  for (var key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      return false;
    }
  }
  return true;
}

function tieDown(sIP, uA){
 const userQuery = {
       Key : {
         "ip" : "192.168.0.1",
         "ua" : "blah"
       },
       TableName: "Table2"
      };
  return ddb.get(userQuery, function(err, data){
   if (err) console.log(err.stack);
  }).promise();
}

This is my first time every working with NodeJS/Lambda/DynamoDB so all help is appreciated, thank you!

Ethan
  • 1,905
  • 2
  • 21
  • 50
  • 1
    This is not a duplicate. They are getting a SyntaxError not the "await is not valid in async function" – 0xLogN Apr 07 '21 at 03:09
  • 1
    @LoganDevine it's the same issue. There's a nested function in Ethan's sample, which is not async. Ethan, take the time to learn about Promises and how async/await relates to them. Having both `await` and `.then()` on a single line means you fundamentally misunderstand how they work together. – Evert Apr 07 '21 at 03:10
  • @Evert: The error would be different usually? – 0xLogN Apr 07 '21 at 03:11
  • @Evert I suppose this was not the best tutorial to learn from then? Even though she is a doctor, https://youtu.be/f7o8RV3Edck?t=859 (regarding `await` and `then` on a single line) – Ethan Apr 07 '21 at 03:36
  • 1
    @LoganDevine could just be a different javascript engine or different contexts throwing different kinds of errors. Fundamentally the problem is the same, even if the literal error message is different. – Evert Apr 07 '21 at 04:14
  • 1
    @Ethan, I can't really give you a great recommendation for a tutorial. My take on this is that authoritative documentation like MDN tend to be better sources than youtube videos, but different people learn in different ways. – Evert Apr 07 '21 at 04:16

0 Answers0