'AWS Lambda - NodeJS - nested Await / Async not returning

Having this small Lambda-Function:

module.exports.handler = async( data , ctx, cb) => {
 console.log("start");
 createSth();
};


async function resolveAfter2Seconds(x) {
  console.log("in function: " + x);
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

async function createSth() {
  var x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}

I would have expted the following output

start
in function: 10
10

But I only get

start
in function: 10

So everything after var x = await resolveAfter2Seconds(10); is not executed.

It works outside of aws lambda in a similar setup.

Thanks.



Solution 1:[1]

Add await keyword while calling createSth function. This will allow main function to wait for the execution of the function and won't return

i.e.

module.exports.handler = async( data , ctx, cb) => {
    console.log("start");
    await createSth();
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 manpreet