@@ -68,11 +68,10 @@ export const deployStack = async (stackArtifact: CloudFormationStackArtifact ):
68
68
return result . outputs ;
69
69
} ;
70
70
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 [ ] > => {
72
72
const invocationLogs : InvocationLogs [ ] = [ ] ;
73
- const promises = [ ] ;
74
-
75
- for ( let i = 0 ; i < times ; i ++ ) {
73
+
74
+ const promiseFactory = ( ) : Promise < void > => {
76
75
const invokePromise = lambdaClient
77
76
. invoke ( {
78
77
FunctionName : functionName ,
@@ -86,9 +85,15 @@ export const invokeFunction = async (functionName: string, times: number = 1): P
86
85
throw new Error ( 'No LogResult field returned in the response of Lambda invocation. This should not happen.' ) ;
87
86
}
88
87
} ) ;
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 ;
92
97
93
98
return invocationLogs ;
94
99
} ;
@@ -106,3 +111,10 @@ export const destroyStack = async (app: App, stack: Stack): Promise<void> => {
106
111
quiet : true ,
107
112
} ) ;
108
113
} ;
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