@@ -68,11 +68,10 @@ export const deployStack = async (stackArtifact: CloudFormationStackArtifact ):
6868 return result . outputs ;
6969} ;
7070
71- export const invokeFunction = async ( functionName : string , times : number = 1 ) : Promise < InvocationLogs [ ] > => {
71+ export const invokeFunction = async ( functionName : string , times : number = 1 , invocationMode : 'PARALLEL' | 'SEQUENTIAL' = 'PARALLEL' ) : Promise < InvocationLogs [ ] > => {
7272 const invocationLogs : InvocationLogs [ ] = [ ] ;
73- const promises = [ ] ;
74-
75- for ( let i = 0 ; i < times ; i ++ ) {
73+
74+ const promiseFactory = ( ) : Promise < void > => {
7675 const invokePromise = lambdaClient
7776 . invoke ( {
7877 FunctionName : functionName ,
@@ -86,9 +85,15 @@ export const invokeFunction = async (functionName: string, times: number = 1): P
8685 throw new Error ( 'No LogResult field returned in the response of Lambda invocation. This should not happen.' ) ;
8786 }
8887 } ) ;
89- promises . push ( invokePromise ) ;
90- }
91- await Promise . all ( promises ) ;
88+
89+ return invokePromise ;
90+ } ;
91+
92+ const promiseFactories = Array . from ( { length : times } , ( ) => promiseFactory ) ;
93+ const invocation = invocationMode == 'PARALLEL'
94+ ? Promise . all ( promiseFactories . map ( factory => factory ( ) ) )
95+ : chainPromises ( promiseFactories ) ;
96+ await invocation ;
9297
9398 return invocationLogs ;
9499} ;
@@ -106,3 +111,10 @@ export const destroyStack = async (app: App, stack: Stack): Promise<void> => {
106111 quiet : true ,
107112 } ) ;
108113} ;
114+
115+ const chainPromises = async ( promiseFactories : ( ( ) => Promise < void > ) [ ] ) : Promise < void > => {
116+ let chain = Promise . resolve ( ) ;
117+ promiseFactories . forEach ( factory => chain = chain . then ( factory ) ) ;
118+
119+ return chain ;
120+ } ;
0 commit comments