@@ -5,9 +5,9 @@ export interface PromiseDecoratorCfg<RES = any, PARAMS = any> {
55
66 /**
77 * Called BEFORE the original function.
8- * Returns void .
8+ * If Promise is returned - it will be awaited .
99 */
10- beforeFn ?: ( r : PromiseDecoratorResp < PARAMS > ) => void
10+ beforeFn ?: ( r : PromiseDecoratorResp < PARAMS > ) => void | Promise < void >
1111
1212 /**
1313 * Called just AFTER the original function.
@@ -73,77 +73,71 @@ export function _createPromiseDecorator<RES = any, PARAMS = any>(
7373 // console.log(`@${cfg.decoratorName} called inside function`)
7474 const started = Date . now ( )
7575
76- return (
77- Promise . resolve ( )
78- // Before function
79- . then ( ( ) => {
80- // console.log(`@${cfg.decoratorName} Before`)
81- if ( cfg . beforeFn ) {
82- return cfg . beforeFn ( {
83- decoratorParams,
84- args,
85- key,
86- target,
87- decoratorName,
88- started,
89- } )
90- }
76+ try {
77+ // Before function
78+ // console.log(`@${cfg.decoratorName} Before`)
79+ if ( cfg . beforeFn ) {
80+ await cfg . beforeFn ( {
81+ decoratorParams,
82+ args,
83+ key,
84+ target,
85+ decoratorName,
86+ started,
9187 } )
92- // Original function
93- . then ( ( ) => originalMethod . apply ( this , args ) )
94- . then ( res => {
95- // console.log(`${cfg.decoratorName} After`)
96- const resp : PromiseDecoratorResp < PARAMS > = {
97- decoratorParams,
98- args,
99- key,
100- target,
101- decoratorName,
102- started,
103- }
104-
105- if ( cfg . thenFn ) {
106- res = cfg . thenFn ( {
107- ...resp ,
108- res,
109- } )
110- }
111-
112- cfg . finallyFn ?.( resp )
113-
114- return res
88+ }
89+
90+ // Original function
91+ let res = await originalMethod . apply ( this , args )
92+
93+ // console.log(`${cfg.decoratorName} After`)
94+ const resp : PromiseDecoratorResp < PARAMS > = {
95+ decoratorParams,
96+ args,
97+ key,
98+ target,
99+ decoratorName,
100+ started,
101+ }
102+
103+ if ( cfg . thenFn ) {
104+ res = cfg . thenFn ( {
105+ ...resp ,
106+ res,
115107 } )
116- . catch ( err => {
117- console . error ( `@${ decoratorName } ${ methodSignature } catch:` , err )
118-
119- const resp : PromiseDecoratorResp < PARAMS > = {
120- decoratorParams,
121- args,
122- key,
123- target,
124- decoratorName,
125- started,
126- }
127-
128- let handled = false
129-
130- if ( cfg . catchFn ) {
131- cfg . catchFn ( {
132- ...resp ,
133- err,
134- } )
135- handled = true
136- }
137-
138- cfg . finallyFn ?.( resp )
139-
140- if ( ! handled ) {
141- throw err // rethrow
142- }
108+ }
109+
110+ cfg . finallyFn ?.( resp )
111+
112+ return res
113+ } catch ( err ) {
114+ console . error ( `@${ decoratorName } ${ methodSignature } catch:` , err )
115+
116+ const resp : PromiseDecoratorResp < PARAMS > = {
117+ decoratorParams,
118+ args,
119+ key,
120+ target,
121+ decoratorName,
122+ started,
123+ }
124+
125+ let handled = false
126+
127+ if ( cfg . catchFn ) {
128+ cfg . catchFn ( {
129+ ...resp ,
130+ err,
143131 } )
144- // es2018 only
145- // .finally(() => {})
146- )
132+ handled = true
133+ }
134+
135+ cfg . finallyFn ?.( resp )
136+
137+ if ( ! handled ) {
138+ throw err // rethrow
139+ }
140+ }
147141 }
148142
149143 return pd
0 commit comments