无服务器:通过调用方法触发并忘记无法正常工作

Man*_*han 8 javascript aws-lambda serverless aws-lambda-edge aws-serverless

我有一个无服务器的 lambda函数,在其中我想触发(调用)一个方法而忘记它

我用这种方式做

   // myFunction1
   const params = {
    FunctionName: "myLambdaPath-myFunction2", 
    InvocationType: "Event", 
    Payload: JSON.stringify(body), 
   };

   console.log('invoking lambda function2'); // Able to log this line
   lambda.invoke(params, function(err, data) {
      if (err) {
        console.error(err, err.stack);
      } else {
        console.log(data);
      }
    });


  // my function2 handler
  myFunction2 = (event) => {
   console.log('does not come here') // Not able to log this line
  }
Run Code Online (Sandbox Code Playgroud)

我注意到,直到并且除非我执行Promise returnin myFunction1,否则它不会触发myFunction2,但是不应该设置lambda InvocationType = "Event"意味着我们希望此操作被触发并忘记并且不关心回调响应吗?

我在这里想念什么吗?

非常感谢您的帮助。

Sur*_*r E 2

myFunction1应该是一个异步函数,这就是为什么该函数在myFunction2可以在lambda.invoke(). 将代码更改为以下内容,然后它应该可以工作:

 const params = {
    FunctionName: "myLambdaPath-myFunction2", 
    InvocationType: "Event", 
    Payload: JSON.stringify(body), 
 };

 console.log('invoking lambda function2'); // Able to log this line
 return await lambda.invoke(params, function(err, data) {
     if (err) {
       console.error(err, err.stack);
     } else {
       console.log(data);
     }
 }).promise();


 // my function2 handler
 myFunction2 = async (event) => {
   console.log('does not come here') // Not able to log this line
 }
Run Code Online (Sandbox Code Playgroud)