From b16be4bcce0514324f42fb579a17aac1186b8748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patricio=20D=C3=ADaz?= Date: Sat, 6 Oct 2018 07:52:30 -0400 Subject: [PATCH 1/2] Index.js can handle is handler.js returns a promise MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patricio Díaz --- template/node/function/handler.js | 4 ++++ template/node/index.js | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/template/node/function/handler.js b/template/node/function/handler.js index 73344f43..a2e0160e 100644 --- a/template/node/function/handler.js +++ b/template/node/function/handler.js @@ -3,3 +3,7 @@ module.exports = (context, callback) => { callback(undefined, {status: "done"}); } + +// module.exports = context => new Promise((resolve, reject) => { +// resolve(undefined) +// }) diff --git a/template/node/index.js b/template/node/index.js index cb9d00d3..e8c2444d 100644 --- a/template/node/index.js +++ b/template/node/index.js @@ -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) { + 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); }; From a8c787d7bb718b149db48602b1170c6eb2dad094 Mon Sep 17 00:00:00 2001 From: Patricio Diaz Date: Sat, 6 Oct 2018 08:44:19 -0400 Subject: [PATCH 2/2] Example for promise returns a valid value Signed-off-by: Patricio Diaz --- .gitignore | 3 +++ template/node/function/handler.js | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9c47dc29..2ee00be9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ build .gradle .idea template + +template +build diff --git a/template/node/function/handler.js b/template/node/function/handler.js index a2e0160e..3a26d225 100644 --- a/template/node/function/handler.js +++ b/template/node/function/handler.js @@ -1,9 +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(undefined) -// }) +module.exports = context => new Promise((resolve, reject) => { + resolve({status: "promise"}) +})