How do you structure sequential AWS service calls within lambda given all the calls are asynchronous?

后端 未结 8 566
暗喜
暗喜 2021-02-02 14:35

I\'m coming from a java background so a bit of a newbie on Javascript conventions needed for Lambda.

I\'ve got a lambda function which is meant to do several AWS tasks i

相关标签:
8条回答
  • 2021-02-02 15:24

    Just saw this old thread. Note that future versions of JS will improve that. Take a look at the ES2017 async/await syntax that streamlines an async nested callback mess into a clean sync like code. Now there are some polyfills that can provide you this functionality based on ES2016 syntax.

    As a last FYI - AWS Lambda now supports .Net Core which provides this clean async syntax out of the box.

    0 讨论(0)
  • 2021-02-02 15:25

    Short answer:

    Use Async / Await — and Call the AWS service (SNS for example) with a .promise() extension to tell aws-sdk to use the promise-ified version of that service function instead of the call back based version.

    Since you want to execute them in a specific order you can use Async / Await assuming that the parent function you are calling them from is itself async.

    For example:

    let snsResult = await sns.publish({
        Message: snsPayload,
        MessageStructure: 'json',
        TargetArn: endPointArn
    }, async function (err, data) {
        if (err) {
            console.log("SNS Push Failed:");
            console.log(err.stack);
            return;
        }
        console.log('SNS push suceeded: ' + data);
        return data;
    }).promise();
    

    The important part is the .promise() on the end there. Full docs on using aws-sdk in an async / promise based manner can be found here: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html

    In order to run another aws-sdk task you would similarly add await and the .promise() extension to that function (assuming that is available).

    For anyone who runs into this thread and is actually looking to simply push promises to an array and wait for that WHOLE array to finish (without regard to which promise executes first) I ended up with something like this:

    let snsPromises = [] // declare array to hold promises
    let snsResult = await sns.publish({
        Message: snsPayload,
        MessageStructure: 'json',
        TargetArn: endPointArn
    }, async function (err, data) {
        if (err) {
            console.log("Search Push Failed:");
            console.log(err.stack);
            return;
        }
        console.log('Search push suceeded: ' + data);
        return data;
    }).promise();
    
    snsPromises.push(snsResult)
    await Promise.all(snsPromises)
    

    Hope that helps someone that randomly stumbles on this via google like I did!

    0 讨论(0)
提交回复
热议问题