From 62407058565bfaedcfb41a2202d933d485d1e263 Mon Sep 17 00:00:00 2001 From: Fabio Braga de Oliveira Date: Thu, 5 Aug 2021 09:35:12 +0200 Subject: [PATCH] Fix Post login trigger according to recommendations in https://github.com/aws-amplify/amplify-cli/pull/7219\#issuecomment-881697658 --- .../src/index.js | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/web/amplify/backend/function/pettracking426fb16cPostConfirmation/src/index.js b/web/amplify/backend/function/pettracking426fb16cPostConfirmation/src/index.js index 80c77ea..c0613d3 100644 --- a/web/amplify/backend/function/pettracking426fb16cPostConfirmation/src/index.js +++ b/web/amplify/backend/function/pettracking426fb16cPostConfirmation/src/index.js @@ -1,14 +1,40 @@ -/* - this file will loop through all js modules which are uploaded to the lambda resource, - provided that the file names (without extension) are included in the "MODULES" env variable. - "MODULES" is a comma-delimmited string. -*/ -const moduleNames = process.env.MODULES.split(','); -const modules = moduleNames.map(name => require(`./${name}`)); +/** + * @fileoverview + * + * This CloudFormation Trigger creates a handler which awaits the other handlers + * specified in the `MODULES` env var, located at `./${MODULE}`. + */ -exports.handler = (event, context, callback) => { - for (let i = 0; i < modules.length; i += 1) { - const { handler } = modules[i]; - handler(event, context, callback); - } -}; +/** + * The names of modules to load are stored as a comma-delimited string in the + * `MODULES` env var. + */ + const moduleNames = process.env.MODULES.split(','); + /** + * The array of imported modules. + */ + const modules = moduleNames.map(name => require(`./${name}`)); + + /** + * This async handler iterates over the given modules and awaits them. + * + * @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-async + * + * @param {object} event + * + * The event that triggered this Lambda. + * + * @returns + * + * The handler response. + */ + exports.handler = async event => { + /** + * Instead of naively iterating over all handlers, run them concurrently with + * `await Promise.all(...)`. This would otherwise just be determined by the + * order of names in the `MODULES` var. + */ + await Promise.all(modules.map(module => module.handler(event))); + return event; + }; + \ No newline at end of file