你如何构造给所有的调用都是异步的内拉姆达连续AWS服务电话?都是、有的、服务电话、拉姆

2023-09-11 10:56:57 作者:冷风浮

我是从一个Java的背景所以有点需要LAMBDA于JavaScript约定的新手。

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

我有这意味着在一个特定的顺序做几次AWS任务的lambda函数,这取决于previous任务的结果。

I've got a lambda function which is meant to do several AWS tasks in a particular order, depending on the result of the previous task.

由于每个任务报告其结果异步,我想知道,如果以正确的方式,确保他们都发生在正确的顺序,并提供给下一个函数的调用一个操作的结果。

Given that each task reports its results asynchronously, I'm wondering if the right way make sure they all happen in the right sequence, and the results of one operation are available to the invocation of the next function.

好像我要invoike在现有功能的回调每个功能,但看起来这将某种深度嵌套的,不知道如果这是正确的方式做到这一点。

It seems like I have to invoike each function in the callback of the prior function, but seems like that will some kind of deep nesting and wondering if that is the proper way to do this.

例如对这些功能需要一个DynamoDB的getItem,通过调用SNS下列以获得一个端点,随后的SNS呼叫来发送消息,随后DynamoDB写

For example on of these functions requires a DynamoDB getItem, following by a call to SNS to get an endpoint, followed by a SNS call to send a message, followed by a DynamoDB write.

什么是正确的方式做,在拉姆达的JavaScript,占所有不同步?

What's the right way to do that in lambda javascript, accounting for all that asynchronicity?

推荐答案

我不知道拉姆达,但你应该看看到节点的异步库的作为方式测序异步函数

I don't know Lambda but you should look into the node async library as a way to sequence asynchronous functions.

异步已经没有你在你的问题中提到的深度嵌套问题使我的生活轻松了许多,我的code更有序。

async has made my life a lot easier and my code much more orderly without the deep nesting issue you mentioned in your question.

典型的异步code可能是这样的:

Typical async code might look like:

async.waterfall([
    function doTheFirstThing(callback) {
         db.somecollection.find({}).toArray(callback);
    },
    function useresult(dbFindResult, callback) {
         do some other stuff  (could be synch or async)
         etc etc etc
         callback(null);
],
function (err) {
    //this last function runs anytime any callback has an error, or if no error
    // then when the last function in the array above invokes callback.
    if (err) { sendForTheCodeDoctor(); }
});

看一看异步多科在上面的链接。有串行,并行,瀑布,以及更多许多有用的功能。异步积极维护和似乎很可靠的。

Have a look at the async doco at the link above. There are many useful functions for serial, parallel, waterfall, and many more. Async is actively maintained and seems very reliable.

祝你好运!