From 95698b7f2cc874069b30ea1238ee5e2611ad0b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Sumis=C5=82awski?= Date: Thu, 11 Jul 2024 10:35:35 +0200 Subject: [PATCH] Disable callbackWaitsForEmptyEventLoop for promise based handlers --- nodejs/packages/cx-wrapper/index.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/nodejs/packages/cx-wrapper/index.ts b/nodejs/packages/cx-wrapper/index.ts index aa9c6ac59a..5de8dff2e6 100644 --- a/nodejs/packages/cx-wrapper/index.ts +++ b/nodejs/packages/cx-wrapper/index.ts @@ -47,24 +47,35 @@ export const handler = (event: any, context: Context, callback: Callback) => { maybePromise.then( value => { diag.debug(`maybePromise succeeded`); + context.callbackWaitsForEmptyEventLoop = false; callback(null, value); diag.debug(`callback called`); }, - (err: Error | string) => { - diag.debug(`maybePromise failed`); - callback(err, null); - diag.debug(`callback called`); + (err: Error | string | null | undefined) => { + if (err === undefined || err === null) { + diag.debug(`maybePromise failed with no error`); + context.callbackWaitsForEmptyEventLoop = false; + callback('handled', null); + diag.debug(`callback called`); + } else { + diag.debug(`maybePromise failed`); + context.callbackWaitsForEmptyEventLoop = false; + callback(err, null); + diag.debug(`callback called`); + } } ); } } catch (err: any) { diag.debug(`handler failed synchronously`); + context.callbackWaitsForEmptyEventLoop = false; callback(err, null); diag.debug(`callback called`); } }, (err: Error | string) => { diag.debug(`loading function failed`); + context.callbackWaitsForEmptyEventLoop = false; callback(err, null) diag.debug(`callback called`); }