Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active July 18, 2018 09:18
Show Gist options
  • Save tarasowski/2997df8485b29b475d784929c13f4a92 to your computer and use it in GitHub Desktop.
Save tarasowski/2997df8485b29b475d784929c13f4a92 to your computer and use it in GitHub Desktop.
#Lambda - Async Code Parallel Execution
// This lambda function will not timeout during a 3sec, since all 3 background processes happening at the same time
const hello1 = () => {
return new Promise(resolve => {
setTimeout(() => resolve(console.log('Hello from Hello1')), 2000)
})
}
const hello2 = () => {
return new Promise(resolve => {
setTimeout(() => resolve(console.log('Hello from Hello2')), 2000)
})
}
const hello3 = () => {
return new Promise(resolve => {
setTimeout(() => resolve(console.log('Hello from Hello3')), 2000)
})
}
exports.lambda_handler = async(event, context, callback) => {
try {
console.log('step 1')
const h1 = hello1()
const h2 = hello2()
const h3 = hello3()
await h1
await h2
await h3
console.log('step 3')
return 'Hello from Lambda'
}
catch (err) {
console.log(err)
return 'Something went wrong'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment