@@ -22,72 +22,72 @@ type FinallyHandler = () => void;
2222 * - error?: any -- if it's a Javascript error, will not be null
2323 */
2424class Do < TResourceType , TReturnVal = void > {
25- private readonly promise : Promise < TReturnVal > ;
26-
27- private constructor ( workload : AsyncWorkload < TReturnVal > ) {
28- this . promise = Promise . resolve ( workload ( ) ) ;
29- }
30-
31- /**
32- * Handle errors from the workload.
33- * If errors are in the shape of a {ResultRecord},
34- * you will get a typed {ResultRecord} as the first parameter.
35- * Otherwise, if it's an unknown error or Javascript error,
36- * you'll get an {any} as the second parameter.
37- * @param errorHandler handle errors, either as a {ResultRecord} or {any}
38- * @returns this
39- */
40- public catch (
41- errorHandler : CatchHandler < TResourceType >
42- ) : Do < TResourceType , TReturnVal > {
43- this . promise . catch ( ( err : any ) => {
44- if ( err instanceof ResultRecord ) {
45- errorHandler ( err , undefined ) ;
46- return ;
47- }
48-
49- errorHandler ( undefined , err ) ;
50- } ) ;
51-
52- return this ;
53- }
54-
55- /**
56- * Run some handler when the function completes, whether the
57- * catch() was hit or not.
58- * @param finallyHandler
59- * @returns this
60- */
61- public finally (
62- finallyHandler : FinallyHandler
63- ) : Do < TResourceType , TReturnVal > {
64- this . promise . finally ( finallyHandler ) ;
65- return this ;
66- }
67-
68- /**
69- * Awaits the internal promise being tracked by the Do instance,
70- * and returns the result. This way, you can await a Do.try
71- * chain, if you need to await for the result inside of another scope,
72- * such as tests.
73- * @returns the result of the promise.
74- */
75- public async getAwaiter ( ) : Promise < TReturnVal > {
76- return this . promise ;
77- }
78-
79- /**
80- * Static factory method for Do class.
81- * Start a workload (sync or async) that you can then
82- * call .catch(catchHandler).finally(finallyHandler) on.
83- * @param workload a sync or async method to wrap
84- * @returns a new instance of Do
85- */
86- public static try < TResourceType , TReturnVal = void > (
87- workload : AsyncWorkload < TReturnVal >
88- ) : Do < TResourceType , TReturnVal > {
89- return new Do ( workload ) ;
90- }
25+ private readonly promise : Promise < TReturnVal > ;
26+
27+ private constructor ( workload : AsyncWorkload < TReturnVal > ) {
28+ this . promise = Promise . resolve ( workload ( ) ) ;
29+ }
30+
31+ /**
32+ * Handle errors from the workload.
33+ * If errors are in the shape of a {ResultRecord},
34+ * you will get a typed {ResultRecord} as the first parameter.
35+ * Otherwise, if it's an unknown error or Javascript error,
36+ * you'll get an {any} as the second parameter.
37+ * @param errorHandler handle errors, either as a {ResultRecord} or {any}
38+ * @returns this
39+ */
40+ public catch (
41+ errorHandler : CatchHandler < TResourceType >
42+ ) : Do < TResourceType , TReturnVal > {
43+ this . promise . catch ( ( err : any ) => {
44+ if ( err instanceof ResultRecord ) {
45+ errorHandler ( err , undefined ) ;
46+ return ;
47+ }
48+
49+ errorHandler ( undefined , err ) ;
50+ } ) ;
51+
52+ return this ;
53+ }
54+
55+ /**
56+ * Run some handler when the function completes, whether the
57+ * catch() was hit or not.
58+ * @param finallyHandler
59+ * @returns this
60+ */
61+ public finally (
62+ finallyHandler : FinallyHandler
63+ ) : Do < TResourceType , TReturnVal > {
64+ this . promise . finally ( finallyHandler ) ;
65+ return this ;
66+ }
67+
68+ /**
69+ * Awaits the internal promise being tracked by the Do instance,
70+ * and returns the result. This way, you can await a Do.try
71+ * chain, if you need to await for the result inside of another scope,
72+ * such as tests.
73+ * @returns the result of the promise.
74+ */
75+ public async getAwaiter ( ) : Promise < TReturnVal > {
76+ return this . promise ;
77+ }
78+
79+ /**
80+ * Static factory method for Do class.
81+ * Start a workload (sync or async) that you can then
82+ * call .catch(catchHandler).finally(finallyHandler) on.
83+ * @param workload a sync or async method to wrap
84+ * @returns a new instance of Do
85+ */
86+ public static try < TResourceType , TReturnVal = void > (
87+ workload : AsyncWorkload < TReturnVal >
88+ ) : Do < TResourceType , TReturnVal > {
89+ return new Do < TResourceType , TReturnVal > ( workload ) ;
90+ }
9191}
9292
9393// #endregion Do
@@ -97,75 +97,75 @@ class Do<TResourceType, TReturnVal = void> {
9797// -----------------------------------------------------------------------------------------
9898
9999class DoSync < TResourceType , TReturnVal = void > {
100- private readonly workload : SyncWorkload < TReturnVal > ;
101- private catchHandler ?: ( err : any ) => void ;
102- private finallyHandler ?: FinallyHandler ;
103-
104- private constructor ( workload : SyncWorkload < TReturnVal > ) {
105- this . workload = workload ;
106- }
107-
108- /**
109- * Add a catch handler to the DoSync call chain.
110- * If errors are in the shape of a {ResultRecord},
111- * you will get a typed {ResultRecord} as the first parameter.
112- * Otherwise, if it's an unknown error or Javascript error,
113- * you'll get an {any} as the second parameter.
114- * @param errorHandler handle errors, either as a {ResultRecord} or {any}
115- */
116- public catch (
117- errorHandler : CatchHandler < TResourceType >
118- ) : DoSync < TResourceType , TReturnVal > {
119- this . catchHandler = ( err : any ) => {
120- if ( err instanceof ResultRecord ) {
121- errorHandler ( err , undefined ) ;
122- return ;
123- }
124-
125- errorHandler ( undefined , err ) ;
126- } ;
127-
128- return this ;
129- }
130-
131- /**
132- * Execute the entire DoSync call chain. For the synchronous version, i.e. DoSync,
133- * you must manually call .execute() for the call chain to be executed.
134- * @returns TReturnVal the value returned from the workload, or undefined if an error occurred.
135- */
136- public execute ( ) : TReturnVal | undefined {
137- try {
138- return this . workload ( ) ;
139- } catch ( e ) {
140- this . catchHandler ?.( e ) ;
141- } finally {
142- this . finallyHandler ?.( ) ;
143- }
144- }
145-
146- /**
147- * Run some handler when the function completes, whether the
148- * catch() was hit or not.
149- * @param finallyHandler
150- * @returns this
151- */
152- public finally (
153- finallyHandler : FinallyHandler
154- ) : DoSync < TResourceType , TReturnVal > {
155- this . finallyHandler = finallyHandler ;
156- return this ;
157- }
158-
159- /**
160- * Static factory method for DoSync. Creates a new DoSync
161- * with the given workload.
162- * @param workload
163- */
164- public static try < TResourceType , TReturnVal = void > (
165- workload : SyncWorkload < TReturnVal >
166- ) {
167- return new DoSync ( workload ) ;
168- }
100+ private readonly workload : SyncWorkload < TReturnVal > ;
101+ private catchHandler ?: ( err : any ) => void ;
102+ private finallyHandler ?: FinallyHandler ;
103+
104+ private constructor ( workload : SyncWorkload < TReturnVal > ) {
105+ this . workload = workload ;
106+ }
107+
108+ /**
109+ * Add a catch handler to the DoSync call chain.
110+ * If errors are in the shape of a {ResultRecord},
111+ * you will get a typed {ResultRecord} as the first parameter.
112+ * Otherwise, if it's an unknown error or Javascript error,
113+ * you'll get an {any} as the second parameter.
114+ * @param errorHandler handle errors, either as a {ResultRecord} or {any}
115+ */
116+ public catch (
117+ errorHandler : CatchHandler < TResourceType >
118+ ) : DoSync < TResourceType , TReturnVal > {
119+ this . catchHandler = ( err : any ) => {
120+ if ( err instanceof ResultRecord ) {
121+ errorHandler ( err , undefined ) ;
122+ return ;
123+ }
124+
125+ errorHandler ( undefined , err ) ;
126+ } ;
127+
128+ return this ;
129+ }
130+
131+ /**
132+ * Execute the entire DoSync call chain. For the synchronous version, i.e. DoSync,
133+ * you must manually call .execute() for the call chain to be executed.
134+ * @returns TReturnVal the value returned from the workload, or undefined if an error occurred.
135+ */
136+ public execute ( ) : TReturnVal | undefined {
137+ try {
138+ return this . workload ( ) ;
139+ } catch ( e ) {
140+ this . catchHandler ?.( e ) ;
141+ } finally {
142+ this . finallyHandler ?.( ) ;
143+ }
144+ }
145+
146+ /**
147+ * Run some handler when the function completes, whether the
148+ * catch() was hit or not.
149+ * @param finallyHandler
150+ * @returns this
151+ */
152+ public finally (
153+ finallyHandler : FinallyHandler
154+ ) : DoSync < TResourceType , TReturnVal > {
155+ this . finallyHandler = finallyHandler ;
156+ return this ;
157+ }
158+
159+ /**
160+ * Static factory method for DoSync. Creates a new DoSync
161+ * with the given workload.
162+ * @param workload
163+ */
164+ public static try < TResourceType , TReturnVal = void > (
165+ workload : SyncWorkload < TReturnVal >
166+ ) {
167+ return new DoSync ( workload ) ;
168+ }
169169}
170170
171171// #endregion DoSync
@@ -174,9 +174,6 @@ class DoSync<TResourceType, TReturnVal = void> {
174174// #region Exports
175175// -----------------------------------------------------------------------------------------
176176
177- export {
178- Do ,
179- DoSync ,
180- } ;
177+ export { Do , DoSync } ;
181178
182179// #endregion Exports
0 commit comments