Skip to content

Add support for async wrapped handlers - #23

Merged
mpuittinen merged 2 commits into
nordcloud:masterfrom
dhm116:bugfix/async-handlers
May 7, 2018
Merged

mpuittinen merged 2 commits into
nordcloud:masterfrom
dhm116:bugfix/async-handlers

Conversation

@dhm116

@dhm116 dhm116 commented May 6, 2018

Copy link
Copy Markdown

This adds some basic support for Node v8 in that it will check if there was a returned result from the wrapped handler and, if that result is also a promise, it will avoid the scenario of returning nested promises.

I tried to add some test coverage that seemed to follow a similar pattern as to the existing tests, but they should only execute when tested with node v8 or up (as they rely upon the async/await features).

@dhm116

dhm116 commented May 7, 2018

Copy link
Copy Markdown
Author

I guess the difference between this and #22 is that this does not require a different mechanism for executing the wrapper (via runAsync), as it will continue to work as expected via the normal run command.

@Enase

Enase commented May 7, 2018

Copy link
Copy Markdown

Could you pls add a test for a case when I use a callback at async handler, see:

const testMod8 = {
  handler: async (event, context, callback) => {
    if (event.test === "success") {
      callback(null, "Success");
    }
    if (event.test === "fail") {
     callback(new Error("Fail"));
    }
  }
};

PS: nobody force me to get rid of a callback in case of async handler. In other words, you never know does function use callback or not.

@mpuittinen
mpuittinen merged commit c7ed84f into nordcloud:master May 7, 2018
@mpuittinen

Copy link
Copy Markdown
Contributor

@dhm116 Thanks for the PR. Included in lambda-wrapper 0.3.0

@dhm116
dhm116 deleted the bugfix/async-handlers branch May 7, 2018 12:34
@dhm116

dhm116 commented May 7, 2018

Copy link
Copy Markdown
Author

@Enase that scenario you're describing would be mixing promises and callbacks, which is never recommended anyhow - I don't mind creating a follow-up PR with additional test coverage around that scenario if others think it would be beneficial...

@mpuittinen

mpuittinen commented May 7, 2018

Copy link
Copy Markdown
Contributor

@Enase I had the same concern as @dhm116 , i.e. it is difficult to see a real-life / best practice scenario where you would use async with callbacks instead of Promises hence I did not include the proposed change in the merge

@Enase

Enase commented May 7, 2018

Copy link
Copy Markdown

@mpuittinen I totally agree with you. but in a real life code is written using async handlers for nodejs6.10. Most of people just changed babel config options and now we have async handlers that use callbacks. My goal is just to point you on a trouble. Just imagine how many people will copy paste the example from serverless blog or Serverless Node.js Starter and many and many other places.
Once again, in general you are absolutely right. I won't going to kill your time anymore.

@mpuittinen

Copy link
Copy Markdown
Contributor

@Enase true, the blog / starter may drive people to "bad" practices. Since it's just a question of test cases (no code changes on the actual modules), I've added those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants