-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node.js template to support promise/async/await #172
Conversation
Signed-off-by: Patricio Diaz <[email protected]>
template/node/function/handler.js
Outdated
@@ -1,5 +1,33 @@ | |||
"use strict" | |||
|
|||
// A simple delay function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code in handler.js usually is an example that shows what the functions is meant to do. That delay function just simulates some delayed payload, to demonstrate how the async/await or promise works. It's just an example, like if we do an API call, or access a database, but without all the requirements for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it still work if you reset this back to how it was?
All of the templates in the project are completely minimal example and this might be confusing for people that are used to the template already.
Yes, it will work as usual. Then a suitable example will be just on the documentation? |
I'll leave it as the original, just adding the promises/async/await support for this template. |
Tell me, we will still support callback as a way to call handler.js? |
I've tested these two combinations: "use strict"
module.exports = (context, callback) => {
callback(null, "done, via CB");
} "use strict"
module.exports = async (context, callback) => {
return "done";
} Both worked as expected 👍 |
Signed-off-by: Patricio Diaz <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Applies #172 to armhf/node64 templates for Node.js Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
Signed-off-by: Patricio Diaz [email protected]
This template introduces the possibility of using Promise or Async/Await on the handler function, in addition of the callback approach used before.
Description
In index.js the value returned by handler.js is checked to figure out if it is the final result or is a Promise so it can be retrieved and handled properly.
Motivation and Context
We need to handle the latest asynchronicity mechanism provided by the language, which are Promises and Async/Await
Fixes issue #92
Which issue(s) this PR fixes
Fixes #92
How Has This Been Tested?
For a Callback version of handler.js
handler.js
For a Promise version of handler.js
handler.js
For a Async/Await version of handler.js
handler.js
Types of changes
Impact to existing users
Hopefully wont break existing functions
Checklist:
git commit -s