Created
August 15, 2022 13:02
-
-
Save dhavaln/f7f0a392e56da96283619d8a4723874b to your computer and use it in GitHub Desktop.
NodeJS Lambda function execution flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doSomething = () => { | |
console.log('Do Something Executed'); | |
} | |
// no callback invoked | |
module.exports.handler = function () { | |
// Lambda finishes AFTER `doSomething()` is invoked | |
setTimeout(() => doSomething(), 1000); | |
}; | |
// callback invoked | |
module.exports.handler = function (event, context, callback) { | |
// Lambda finishes AFTER `doSomething()` is invoked | |
setTimeout(() => doSomething(), 1000); | |
callback(null, "Hello World!"); | |
}; | |
// callback invoked, context.callbackWaitsForEmptyEventLoop = false | |
module.exports.handler = function (event, context, callback) { | |
// Lambda finishes BEFORE `doSomething()` is invoked | |
context.callbackWaitsForEmptyEventLoop = false; | |
setTimeout(() => doSomething(), 2000); | |
setTimeout(() => callback(null, "Hello World!"), 1000); | |
}; | |
// async/await | |
module.exports.handler = async function () { | |
// Lambda finishes BEFORE `doSomething()` is invoked | |
setTimeout(() => doSomething(), 1000); | |
return "Hello World!"; | |
}; | |
// Promise | |
module.exports.handler = function () { | |
// Lambda finishes BEFORE `doSomething()` is invoked | |
setTimeout(() => doSomething(), 1000); | |
return Promise.resolve("Hello World!"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Referenced from https://sequelize.org/docs/v7/other-topics/aws-lambda/