Skip to content
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

Merged
merged 2 commits into from
Nov 12, 2019

Conversation

padiazg
Copy link
Contributor

@padiazg padiazg commented Sep 18, 2019

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

  • I have raised an issue to propose this change (required)

Which issue(s) this PR fixes

Fixes #92

How Has This Been Tested?

For a Callback version of handler.js

$ faas new node-cb --lang node --prefix padiazg

handler.js

"use strict"

// A simple delay function
const delayCallback = cb => {
    const min = 0; // 0 sec
    const max = 2; // 2 sec
    const delay = Math.round((Math.random() * (max - min) + min));
    setTimeout(() => cb(delay),  delay * 1000);
}

// callback version
module.exports = (context, callback) => {
    delayCallback(delay => callback(undefined, {status: context || "callback", delay}))
}
$ faas up -f node-cb.yml
$ echo -n "cb" | faas invoke node-cb -
{"status":"cb","delay":1}

For a Promise version of handler.js

$ faas new node-promise --lang node --prefix padiazg

handler.js

"use strict"

// A simple delay function
const delayCallback = cb => {
    const min = 0; // 0 sec
    const max = 2; // 2 sec
    const delay = Math.round((Math.random() * (max - min) + min));
    setTimeout(() => cb(delay),  delay * 1000);
}

// Uncomment the following line to use it with the promise or async/await versions
const delayPromise = () => new Promise(resolve => delayCallback(delay => resolve(delay)) )

module.exports = context => new Promise((resolve, reject) => {
    delayPromise()
    .then(delay => {
        resolve({status: context || "promise", delay});
    })
})
$ faas up -f node-promise.yml
$ echo -n "prom" | faas invoke node-promise -
{"status":"prom","delay":2}

For a Async/Await version of handler.js

$ faas new node-async-await --lang node --prefix padiazg

handler.js

"use strict"

// A simple delay function
const delayCallback = cb => {
    const min = 0; // 0 sec
    const max = 2; // 2 sec
    const delay = Math.round((Math.random() * (max - min) + min));
    setTimeout(() => cb(delay),  delay * 1000);
}

// Uncomment the following line to use it with the promise or async/await versions
const delayPromise = () => new Promise(resolve => delayCallback(delay => resolve(delay)) )

module.exports = async context => {
    const delay = await delayPromise();
    return {status: context || "async/await", delay}
}
$ faas up -f node-promise.yml
$ echo -n "async" | faas invoke node-async-await -
{"status":"async","delay":0}

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [*] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Version change (see: Impact to existing users)

Impact to existing users

Hopefully wont break existing functions

Checklist:

  • [*] My code follows the code style of this project.
  • [*] My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • [*] I've read the CONTRIBUTION guide
  • [*] I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@padiazg padiazg changed the title Node.js template to support promise/async/await [WIP] Node.js template to support promise/async/await Sep 18, 2019
@@ -1,5 +1,33 @@
"use strict"

// A simple delay function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Contributor Author

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.

Copy link
Member

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.

@padiazg
Copy link
Contributor Author

padiazg commented Oct 29, 2019

Yes, it will work as usual. Then a suitable example will be just on the documentation?

@padiazg
Copy link
Contributor Author

padiazg commented Oct 29, 2019

I'll leave it as the original, just adding the promises/async/await support for this template.

@padiazg
Copy link
Contributor Author

padiazg commented Oct 29, 2019

Tell me, we will still support callback as a way to call handler.js?

@alexellis
Copy link
Member

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]>
@padiazg padiazg changed the title [WIP] Node.js template to support promise/async/await Node.js template to support promise/async/await Nov 12, 2019
Copy link
Member

@alexellis alexellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexellis alexellis merged commit 047269f into openfaas:master Nov 12, 2019
alexellis added a commit that referenced this pull request Nov 12, 2019
Applies #172 to armhf/node64 templates for Node.js

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
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.

handler.js can return a Promise
2 participants