我有一个lambda函数,它将http调用发送到API(比方说'A'
)。收到'A'
后的回复后立即将内容返回给来电者,即10 {s}内的(callback(null, success))
。然后将从API 'A'
获取的数据保存到我的外部API(让我们说'B'
)。
我尝试过如下,但Lambda等待直到事件循环为空(它正在等待第二次http调用的响应)。
我不想将eventLoopWaitEmpty设置为false,因为它会冻结eventloop并在下次调用时执行。
request.get({url: endpointUrlA},
function (errorA, responseA, bodyA) {
callback(null, "success");
request.post({url: endpointUrlB,
body: bodyA,
json: true}, function(errorB, responseB, bodyB){
//Doesn't want to wait for this response
});
/* Also tried the callback(null, "success"); here too
});
有人对我如何实现这一点有任何想法?谢谢!
PS - Btw I read the Previous similar questions doesn't seems to clear with those.
答案 0 :(得分:4)
这似乎是一个很好的候选者,可以将这个lambda分解成两个带有一些支持代码的lambdas。
success
状态。这有几个好处。
首先,您不再需要长时间运行的lambda等待可能需要返回的第二个系统。 其次,你在后台异步做事。
请查看this blog post,了解其在实践中的运作方式。