-
Notifications
You must be signed in to change notification settings - Fork 26
[WIP] Refactor to support Promises and Async/Await #9
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules/** | ||
build | ||
|
||
#template |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,64 @@ | ||
"use strict" | ||
|
||
module.exports = (event, context) => { | ||
let err; | ||
// callback version | ||
const delayCallback = cb => { | ||
const min = 1; // 1 sec | ||
const max = 5; // 5 sec | ||
const delay = Math.round((Math.random() * (max - min) + min)); | ||
setTimeout(() => cb(delay), delay * 1000); | ||
} | ||
|
||
module.exports = (event, context, next) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we use express we are using a framework, which is in control of the execution of our code, not us. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't line the next() parameter, it's confusing. mixes express and faas programming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know the use of the next() function it is confusing, but is something a express user is used to. I'm open to suggestions on how to solve the callback call in a different way. I already suggested that we need to drop support for it, but until that happens there must be a way to use it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other way is to pass the res object to the handler function, with passes the control of the flow to it. I think the function main code has to be in control of that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or not use callbacks ;-) I rebuilt a brand new template - thanks for your inspiration. will publish soon - I am testing right now. It also works differently, more inlined with lambda.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, that's something I already asked about, what happens if we don't use callbacks at all |
||
delayCallback(delay => { | ||
const result = { | ||
status: "You said: " + JSON.stringify(event.body), | ||
delay | ||
}; | ||
|
||
context | ||
.status(200) | ||
.succeed(result); | ||
|
||
next(); | ||
}) | ||
} | ||
|
||
// Uncomment the following line to use it in the promise or async/await versions | ||
// const delayPromise = () => new Promise((resolve, reject) => delayCallback(delay => resolve(delay)) ) | ||
|
||
// Promise version | ||
/* | ||
module.exports = (event, context) => new Promise((resolve, reject) => { | ||
delayPromise() | ||
.then(delay => { | ||
const result = { | ||
status: "You said: " + JSON.stringify(event.body), | ||
delay | ||
}; | ||
|
||
context | ||
.status(200) | ||
.succeed(result); | ||
|
||
return resolve(context); | ||
}) | ||
}); | ||
*/ | ||
|
||
// async/await version | ||
/* | ||
module.exports = async (event, context) => { | ||
const delay = await delayPromise(); | ||
|
||
const result = { | ||
status: "You said: " + JSON.stringify(event.body) | ||
status: "You said: " + JSON.stringify(event.body), | ||
delay | ||
}; | ||
|
||
context | ||
.status(200) | ||
.succeed(result); | ||
|
||
return context; | ||
} | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now part of the main template, was that intended or was it a mistake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see handler.js as an example on how to build the code for the serverless function. As the main motivation for this refactoring is to handle asynchronicity, I use that function to simulate some load, and show how to take advantage of this new feature. But it's just a proposal, you can keep it or use the original example.