How to wait for async actions inside AWS Lambda? How to wait for async actions inside AWS Lambda? node.js node.js

How to wait for async actions inside AWS Lambda?


The life of a dev is constantly changing and we now have NodeJS 8 on lambda. For anyone looking at this now check out:

Lambda node 8.10 vs node 6.10 comparison:https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/

Basics of JS async:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Even more aws sdk examples: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html

Details on wtf the .promise() method is in the first link: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Request.html#promise-property

Here is my take at a basic example (try pasting into your own lambda):

exports.handler = async (event) => {        function wait(){        return new Promise((resolve, reject) => {            setTimeout(() => resolve("hello"), 2000)        });    }        console.log(await wait());    console.log(await wait());    console.log(await wait());    console.log(await wait());    console.log(await wait());    console.log(await wait());        return 'exiting'};