Upgrade node environment to Node.js 7.6.0+ (#151)
Upgrades the node environment to NodeJS 7.6.0. Functions can now use async/await and promises; they can return a promise instead of using a callback. The change preserves compatibility with the callback style. * Upgrade node environment to Node.js 7.6.0+ * Bump package version * Preserve compatibility for callbacks * Cleanup * Re-add hello callback example * Make sure not returning won't blow everything up * Update stock example with request promise
This commit is contained in:
committed by
Soam Vasani
parent
e1cb5f6e09
commit
9bdaf3c74e
@@ -75,11 +75,13 @@ app.all('/', function (req, res) {
|
||||
res.status(500).send("Generic container: no requests supported");
|
||||
return;
|
||||
}
|
||||
|
||||
const context = {
|
||||
request: req,
|
||||
response: res
|
||||
// TODO: context should also have: URL template params, query string
|
||||
};
|
||||
|
||||
function callback(status, body, headers) {
|
||||
if (!status)
|
||||
return;
|
||||
@@ -90,18 +92,32 @@ app.all('/', function (req, res) {
|
||||
}
|
||||
res.status(status).send(body);
|
||||
}
|
||||
try {
|
||||
//
|
||||
// Customizing the request context
|
||||
//
|
||||
// If you want to modify the context to add anything to it,
|
||||
// you can do that here by adding properties to the context.
|
||||
//
|
||||
userFunction(context, callback);
|
||||
} catch(e) {
|
||||
console.log(`Function error: ${e}`);
|
||||
callback(500, "Internal server error")
|
||||
|
||||
//
|
||||
// Customizing the request context
|
||||
//
|
||||
// If you want to modify the context to add anything to it,
|
||||
// you can do that here by adding properties to the context.
|
||||
//
|
||||
|
||||
let functionProm;
|
||||
if (userFunction.length === 1) { // One argument (context)
|
||||
// Make sure their function returns a promise
|
||||
Promise.resolve(userFunction(context)).then(function({ status, body, headers }) {
|
||||
callback(status, body, headers);
|
||||
}).catch(function(err) {
|
||||
console.log(`Function error: ${e}`);
|
||||
callback(500, "Internal server error");
|
||||
});
|
||||
} else { // 2 arguments (context, callback)
|
||||
try {
|
||||
userFunction(context, callback);
|
||||
} catch (err) {
|
||||
console.log(`Function error: ${e}`);
|
||||
callback(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
app.listen(argv.port);
|
||||
|
||||
Reference in New Issue
Block a user