"Do not wait for empty event loop" middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda
<img src="https://badge.fury.io/js/%40middy%2Fdo-not-wait-for-empty-event-loop.svg" alt="npm version" style="max-width:100%;">
<img src="https://packagephobia.com/badge?p=@middy/do-not-wait-for-empty-event-loop" alt="npm install size" style="max-width:100%;">
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
<a href="https://standardjs.com/">
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
You can read the documentation at: https://middy.js.org/docs/middlewares/do-not-wait-for-empty-event-loop
This middleware sets
context.callbackWaitsForEmptyEventLoop
property to false
.
This will prevent Lambda from timing out because of open database connections, etc.Install
To install this middleware you can use NPM:npm install --save @middy/do-not-wait-for-empty-event-loop
Options
By default the middleware sets thecallbackWaitsForEmptyEventLoop
property to false
only in the before
phase,
meaning you can override it in handler to true
if needed. You can set it in all steps with the options:runOnBefore
(boolean) (defaulttrue
) - sets property before running your handlerrunOnAfter
(boolean) (defaultfalse
)runOnError
(boolean) (defaultfalse
)
Sample usage
import middy from '@middy/core'
import doNotWaitForEmptyEventLoop from '@middy/do-not-wait-for-empty-event-loop'
const handler = middy((event, context) => {
return {}
})
handler.use(doNotWaitForEmptyEventLoop({runOnError: true}))
// When Lambda runs the handler it gets context with callbackWaitsForEmptyEventLoop property set to false
handler(event, context, (_, response) => {
t.is(context.callbackWaitsForEmptyEventLoop,false)
})