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 73344f43..3a26d225 100644 --- a/template/node/function/handler.js +++ b/template/node/function/handler.js @@ -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"}) +}) 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); };