1

I was having problems running some code I wrote using AWS Lambda with NodeJS. It took me some time to narrow my issue down but eventually I noticed that if I call the callback function from a different context than the context of handler function, then the response value is null.

Here's the simple function I used to test that

exports.handler = async (event, context, callback) => {
  callback (null,{
        statusCode: 201,
        headers: {
          "Content-Type": "text/html"
        },
        body: {}
  });
}

If I run a test event with this code (test script is an empty JSON) then the result is simply:

Response:
{
  "statusCode": 201,
  "headers": {
    "Content-Type": "text/html"
  },
  "body": {}
}

However if I only force the callback function to be called in a different context as in the example below:

exports.handler = async (event, context, callback) => {    
    setTimeout(function() {
        callback (null,{   // <-- callback is now called in timer context
            statusCode: 201,
            headers: {
              "Content-Type": "text/html"
            },
            body: {}
      })}, 5000);
}

Then running the exact same test event (empty JSON) now results in a null response:

Response:
null

Now this seems to me like a very basic functionality, I just assume I am missing something here. Can anyone help pinpoint my error? or explain how the response value can contain the required values even if callback is called in a different context?? Thanks!

Synchronous code with working result Asynchronous code with null response Test event - empty JSON object

Update #1 Tested one more time with the use of a promise:

Another test with promise

6
  • 1
    The question lacks stackoverflow.com/help/mcve . This totally depends on how you use the function. It doesn't depend on a 'context'. Snippet 1 is synchronous. Snippet 2 is asynchronous. You don't use promises, so async doesn't do any good. Commented Jan 28, 2019 at 20:12
  • Thanks for your feedback but how does that lack any information? I mentioned that I only test it with the built-in test event functionality in Lambda using an empty JSON object. You can literally recreate it your self in less than 2 minutes. Commented Jan 28, 2019 at 20:25
  • And yes, you are right - one version is synchronous and the other is asynchronous. I expected both to work but the asynchronous method does not. I have no idea why and that is why I'm asking here for help Commented Jan 28, 2019 at 20:26
  • I see, so the function is fed directly to Lambda. I don't use Lambda myself but I would expect it to understand callback parameter in this case. Did you try to stick to promises and return a value from async instead of using a callback? Commented Jan 28, 2019 at 21:26
  • Thanks @estus I just tried using a promise and added a screenshot of that as well to the question details (at the bottom). It still does not work and response is null... Commented Jan 28, 2019 at 21:41

1 Answer 1

1

Your code will return immediately before the callbacks are invoked. That's why it works in the first example.

Try adding await to the front of your Promise example so that node completes the work before moving on.

const value = await delay(5000)
callback(null, value)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I tried adding the await as you suggest. I edited your suggested code a little to what I tried and it indeed worked. So thanks! Now I will try to apply this to my original problem and see how that works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.