Middleware for the middy framework that allows to easily disable the wait for empty event loop in a Lambda function

Downloads in past


2,98053.2.0a day ago4 years agoMinified + gzip package size for @middy/do-not-wait-for-empty-event-loop in KB


Middy do-not-wait-for-empty-event-loop middleware
Middy logo

"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.


To install this middleware you can use NPM:
npm install --save @middy/do-not-wait-for-empty-event-loop


By default the middleware sets the callbackWaitsForEmptyEventLoop 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) (default true) - sets property before running your handler
  • runOnAfter (boolean) (default false)
  • runOnError (boolean) (default false)

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) => {

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.


Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.


Licensed under MIT License. Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the Middy team.
FOSSA Status