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

Handler.js can return a promise #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build
.gradle
.idea
template

template
build
10 changes: 7 additions & 3 deletions template/node/function/handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"use strict"

module.exports = (context, callback) => {
callback(undefined, {status: "done"});
}
//module.exports = (context, callback) => {
// callback(undefined, {status: "done"});
//}

module.exports = context => new Promise((resolve, reject) => {
resolve({status: "promise"})
})
23 changes: 15 additions & 8 deletions template/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,31 @@ const getStdin = require('get-stdin');
const handler = require('./function/handler');

getStdin().then(val => {
handler(val, (err, res) => {
const cb = (err, res) => {
if (err) {
return console.error(err);
}
if(isArray(res) || isObject(res)) {
if (!res) {
return;
}
if(Array.isArray(res) || isObject(res)) {
console.log(JSON.stringify(res));
} else {
process.stdout.write(res);
}
});
} // cb ...

const result = handler(val, cb);
if (result instanceof Promise) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You're missing a branch where the result is not a Promise. I think that will break backwards compatibility where someone updates their existing function to the new template or they write a new function the old way expecting it to still work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. The portion of code responsible of returning the result of the function is now packet in cb, which is passed to handler. If you are running a "legacy" function cb will be called, the result sent to the GW and all ends there.
But if in turn you are running a function that returns a Promise, it will be "unresolved" in index.js and that's why we check if it's a Promise, and resolve it to return a result.

result
.then(data => cb(undefined, data))
.catch(err => cb(err, undefined))
;
}
}).catch(e => {
console.error(e.stack);
console.error(e.stack);
});

const isArray = (a) => {
return (!!a) && (a.constructor === Array);
};

const isObject = (a) => {
return (!!a) && (a.constructor === Object);
};