by jtdev · 6y ago · 0 comments
func lambdaHandler(event: Event, context: Context) { // your lambda handler logic... }
What’s wrong with this approach, which is how lambda handlers are written in every other language with the exception of JavaScript - let’s not pretend that JavaScript is a good language design reference.
Look at this Go example In comparison... simple, readable, intuitive:
package main
import ( "fmt" "context" "github.com/aws/aws-lambda-go/lambda" )
type MyEvent struct { Name string `json:"name"` }
func HandleRequest(ctx context.Context, name MyEvent) (string, error) { return fmt.Sprintf("Hello %s!", name.Name ), nil }
func main() { lambda.Start(HandleRequest) }
Functions and closures-that-don't-reference-their-environment are exactly equivalent in both languages, from a quick look? It's your choice as a developer which to use.
This particular pattern in the case of Lambda is specifically for providing a closure which is called later and repeatedly - it's not waiting for a request and then calling the closure once and then returning.