@@ -25,32 +25,37 @@ class BaseClient {
2525 }
2626
2727 // Given an extractor configuration, initialize all the necessary extractors
28- initializeExtractors ( extractorConfig , commonExtractorArgs ) {
29- let allExtractorsValid = true ;
30-
28+ async initializeExtractors ( extractorConfig , commonExtractorArgs ) {
29+ // Loop to initialize the extractors
3130 extractorConfig . forEach ( ( curExtractorConfig ) => {
3231 const { label, type, constructorArgs } = curExtractorConfig ;
3332 logger . debug ( `Initializing ${ label } extractor with type ${ type } ` ) ;
3433 const ExtractorClass = this . extractorClasses [ type ] ;
35-
3634 try {
3735 const newExtractor = new ExtractorClass ( { ...commonExtractorArgs , ...constructorArgs } ) ;
38-
39- if ( newExtractor . validate ) {
40- const isExtractorValid = newExtractor . validate ( ) ;
41- allExtractorsValid = ( allExtractorsValid && isExtractorValid ) ;
42- if ( isExtractorValid ) {
43- logger . debug ( `Extractor ${ label } PASSED CSV validation` ) ;
44- } else {
45- logger . debug ( `Extractor ${ label } FAILED CSV validation` ) ;
46- }
47- }
48-
4936 this . extractors . push ( newExtractor ) ;
5037 } catch ( e ) {
5138 throw new Error ( `Unable to initialize ${ label } extractor with type ${ type } : ${ e . message } ` ) ;
5239 }
5340 } ) ;
41+ // For validation, we are looping over extractors and performing an async operation on each.
42+ // We need to loop without forEach (since forEach is sequential).
43+ // Using Reduce to compute the validity of all extractors
44+ const allExtractorsValid = await this . extractors . reduce ( async ( curExtractorsValid , curExtractor ) => {
45+ const { name } = curExtractor . constructor ;
46+
47+ if ( curExtractor . validate ) {
48+ logger . debug ( `Validating ${ name } ` ) ;
49+ const isExtractorValid = await curExtractor . validate ( ) ;
50+ if ( isExtractorValid ) {
51+ logger . debug ( `Extractor ${ name } PASSED CSV validation` ) ;
52+ } else {
53+ logger . warn ( `Extractor ${ name } FAILED CSV validation` ) ;
54+ }
55+ return ( curExtractorsValid && isExtractorValid ) ;
56+ }
57+ return curExtractorsValid ;
58+ } , true ) ;
5459
5560 if ( allExtractorsValid ) {
5661 logger . info ( 'Validation succeeded' ) ;
0 commit comments