简体   繁体   中英

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.

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

ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM