Skip to content

Async handler support for nodejs8.10 - #21

Closed
Enase wants to merge 2 commits into
nordcloud:masterfrom
Enase:master
Closed

Enase wants to merge 2 commits into
nordcloud:masterfrom
Enase:master

Conversation

@Enase

@Enase Enase commented Apr 26, 2018

Copy link
Copy Markdown

Just a suggestion of how to fix. No tests etc.
see #20

Enase pushed a commit to Enase/serverless-mocha-plugin that referenced this pull request Apr 26, 2018
@sonya

sonya commented May 4, 2018

Copy link
Copy Markdown

I think this patch would necessitate people who use the 8.10 runtime to write their handlers as promises, which they might not be doing. Instead of checking for the callback (which I believe can be injected independently of the handler type), would it make sense to just check if the handler is a promise? I think the version may not even be relevant in that case.

I'm very much interested in having support for async/await in this library also.

@Enase

Enase commented May 5, 2018

Copy link
Copy Markdown
Author

this patch would necessitate people who use the 8.10 runtime to write their handlers as promises

it's not true. see if (process.version === 'v8.10.0' && !cb) {
in case if callback is NOT provided only

@sonya

sonya commented May 5, 2018

Copy link
Copy Markdown

That line checks whether the callback parameter was provided to the lambda function, not whether the lambda function responds to a callback parameter.

If I implemented my lambda function as

exports.handler = (event, context, callback) => {
  callback(null);
}

then invoked it with

wrapped.run(event)

this would pass the if check and proceed to fail with then is not a function.

Likewise, if my lambda function was implemented as

exports.handler = async (event) => {
  return true;
}

and invoked using

wrapped.run(event, context, callback)

this would fail the if check and proceed to run using the old lambda-wrapper behavior.

Additionally, checking the current version against a string is fragile. Someone whose local environment is running node 8.11 for whatever reason would not be supported by this change.

My earlier suggestion (which was worded obliquely) was to remove the check for the version number and instead check whether the lambda is a Promise, which can be done by checking whether .then is a function.

@Enase

Enase commented May 5, 2018

Copy link
Copy Markdown
Author

Your explanation doesn't make sense for me because of two reasons:

  • async handler doesn't force me to use return instead of callback so that "thenable" check doesn't make sense
  • wrapped.run is a client code, library is not responsible for a way developers use it. but I agree that it would be better to have separate method that says wrapped.runAsync

checking the current version against a string is fragile

that's true, that's why this pull request just a suggestion or demo, at least I hope it wont be merged as is. btw I have updated verification with regex

@Enase

Enase commented May 5, 2018

Copy link
Copy Markdown
Author

closed because of #22

@Enase Enase closed this May 5, 2018
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