When we use async don’t you need to use callback?

Asked

Viewed 46 times

1

I have the following function in V10.6 nodejs:

#func-1
module.exports.funcOne = (event, context, callback) => { 
  callback(null, { message: 'funcOne', event });
};

#func-2
module.exports.funcTwo = async (event, context) => { 
  console.log("value1 = " + event.key1);
  console.log("value2 = " + event.key2);  
  return 'some success message';
};

When I turn the remote on the terminal: sls invoke local --function funcOne have the answer:

{
    "message": "funcOne",
    "event": ""
}

And when I turn the remote sls invoke local --function funcTwo have the answer:

value1 = undefined
value2 = undefined

I can’t see some success message;

I’m following the documentation of the AWS for lambda functions and Nodejs trying to understand asynchronous functions with callbacks.

Could someone explain why I don’t see the string in the second example?

  • So, I can’t tell you with 100% certainty if this is it, but from my experience with JS, when you use async, for you to have the output of the function that returns an async, you need to use await, so if you were to call this function normally, you would use Let x = await funcTwo(), for example, and x would return it. I don’t know if that’s the case, but..

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.