signUp with amazon-cognito-identity-js.I use Serverless framework to pack and deploy. The runtime is Node 12
+++++++
const wrapperHandler: Handler<CognitoUserPoolEvent> = async (
event,
context,
callback
) => {
let error = null;
try {
await myAsyncFunc();
} catch (e) {
error = e;
}
callback(error, event);
};
Everything works fine, it can return the error to the actual endpoint lambda which will then be returned, if no error, the logic will be executed.
However, this warning is pretty annoying.
The code is for preSignUp in CloudWatch
WARNING: Callback/response already delivered. Did your function invoke the callback and also return a promise? For more details, see: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
In the code, I didn't return anything before calling the callback, why would this happen? and how to solve it.
As your error message what did you get, you are mixing callback style with async/await style, then it throws a warning.
I prefer use async/await. This mean, handler function always is a async function (with async keyword), then instead of call callback function, just return the result, and you no need callback parameter in handler function.
In error case, just throw the error (without try/catch block).
const wrapperHandler: Handler<CognitoUserPoolEvent> = async (
event,
context,
// callback
) => {
// let error = null;
try {
await myAsyncFunc();
} catch (e) {
// error = e;
// Do something with your error
throw e;
}
// callback(error, event);
return event; // just return result for handler function
};
In simple:
const wrapperHandler: Handler<CognitoUserPoolEvent> = async (
event,
context,
) => {
await myAsyncFunc();
return event;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With