1

I think the AWS Lambda docs are misleading, but I wanted to check here first.

Their doc https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html states that "If your code performs an asynchronous task, return a promise to ensure that it finishes running." And their code examples all demonstrate returning a promise directly. This seems to indicate that the following would be incorrect:

exports.handler = async (event) => {
    const data = await somePromise();
    return data;
};

But in reality this works perfectly fine. I feel like their doc entirely misses the point of async/await - if you're going to return a promise, then you don't actually need to mark your function as async, right?

Edit: What I'm looking for is a direct answer on whether the specific assertion that "If your code performs an asynchronous task, return a promise to ensure that it finishes running." is incorrect. I'm fairly sure of the answer, but wanted to get confirmation from a javascript expert, such that I can include this SO post if I contact AWS to get the doc updated. I feel that this is fairly important, given that Lambda enables a lot of JS novices to easily run JS code, and so the advice/boilerplate they're given should be better.

jameslol
  • 1,451
  • 13
  • 15
  • 1
    Possible duplicate of [async/await implicitly returns promise?](https://stackoverflow.com/questions/35302431/async-await-implicitly-returns-promise) – jlewkovich Jun 14 '19 at 03:54
  • I don't think it's a duplicate. To the best of my ability, it confirms that my understanding of async functions is correct. But what I'm looking for is a direct answer on whether the specific assertion that "If your code performs an asynchronous task, return a promise to ensure that it finishes running." is incorrect, given that it's pointless to return a promise inside an async function – jameslol Jun 14 '19 at 03:59
  • You might return something from a library/API call that returns a promise after you've awaited something previously. To me that doesn't seem "pointless". – Dave Newton Jun 14 '19 at 04:19
  • It's not pointless to return a promise from a function. It *is* pointless to declare a function as `async` if you're just going to be explicitly returning a promise, as in their S3 example. You could make the function not `async` and the result would be the same. Sure, it could be useful to have an async function that awaits something and then also returns another promise. But not for a single promise, as in their example. – jameslol Jun 14 '19 at 04:23
  • What I am saying (and requesting confirmation of) is that it's not true that you have to return a promise from an async function "to ensure that it finishes running" – jameslol Jun 14 '19 at 04:25
  • @jameslol Indeed, you don't have to explicitly return a promise from an `async` function - you can let the JS runtime implicitly wrap your return value in a promise, however it is entirely valid JS to explicitly return a promise from an `async` function, so there is no *"error in their docs"* as you put it. I agree that the examples could be more realistic of why an `async` function is desirable, and that the wording leading up to the examples could be improved - but there is no *error*. Both examples could be marked as non-`async`, and still work with zero changes to their function bodies. – Sven Jun 14 '19 at 04:45
  • The statement "return a promise to ensure that it finishes running" very directly implies that if you don't return a promise, your asynchronous tasks won't finish running. Do you agree with that? – jameslol Jun 14 '19 at 05:57

0 Answers0