@@ -728,11 +728,14 @@ class JoSk {
728
728
* Cancel (abort) current interval timer.
729
729
* Must be called in a separate event loop from `.setInterval()`
730
730
* @name clearInterval
731
- * @param {string } timerId - Unique function (task) identification as a string, returned from `.setInterval()`
731
+ * @param {string|Promise<string> } timerId - Unique function (task) identification as a string, returned from `.setInterval()`
732
732
* @param {function } [callback] - optional callback
733
733
* @returns {Promise<boolean> } - `true` if task cleared, `false` if task doesn't exist
734
734
*/
735
735
async clearInterval ( timerId ) {
736
+ if ( typeof timerId === 'object' && timerId instanceof Promise ) {
737
+ return await this . __remove ( await timerId ) ;
738
+ }
736
739
return await this . __remove ( timerId ) ;
737
740
}
738
741
@@ -742,11 +745,14 @@ class JoSk {
742
745
* Cancel (abort) current timeout timer.
743
746
* Must be called in a separate event loop from `.setTimeout()`
744
747
* @name clearTimeout
745
- * @param {string } timerId - Unique function (task) identification as a string, returned from `.setTimeout()`
748
+ * @param {string|Promise<string> } timerId - Unique function (task) identification as a string, returned from `.setTimeout()`
746
749
* @param {function } [callback] - optional callback
747
750
* @returns {Promise<boolean> } - `true` if task cleared, `false` if task doesn't exist
748
751
*/
749
752
async clearTimeout ( timerId ) {
753
+ if ( typeof timerId === 'object' && timerId instanceof Promise ) {
754
+ return await this . __remove ( await timerId ) ;
755
+ }
750
756
return await this . __remove ( timerId ) ;
751
757
}
752
758
@@ -841,6 +847,7 @@ class JoSk {
841
847
}
842
848
throw error ;
843
849
}
850
+
844
851
const date = new Date ( ) ;
845
852
const timestamp = + date ;
846
853
@@ -870,6 +877,7 @@ class JoSk {
870
877
return true ;
871
878
} ;
872
879
880
+ let hasError ;
873
881
let returnedPromise ;
874
882
try {
875
883
if ( task . isInterval === false ) {
@@ -880,6 +888,7 @@ class JoSk {
880
888
} catch ( removeError ) {
881
889
this . _debug ( `[${ task . uid } ] [__execute] [__remove] has thrown an exception; Check connection with StorageAdapter; removeError:` , removeError ) ;
882
890
}
891
+
883
892
if ( isRemoved === true ) {
884
893
returnedPromise = originalTask ( ready ) ;
885
894
}
@@ -889,41 +898,51 @@ class JoSk {
889
898
890
899
if ( returnedPromise && returnedPromise instanceof Promise ) {
891
900
await returnedPromise ;
901
+ } else {
902
+ return ;
892
903
}
893
904
} catch ( taskExecError ) {
905
+ hasError = true ;
894
906
this . __errorHandler ( taskExecError , 'Exception during task execution' , 'An exception was thrown during task execution' , task . uid ) ;
895
907
}
896
908
897
- await ready ( ) ;
898
- } else {
899
- await this . adapter . update ( task , new Date ( Date . now ( ) + this . zombieTime ) ) ;
900
- this . tasks [ task . uid ] = function ( ) { } ;
901
- this . tasks [ task . uid ] . isMissing = true ;
902
-
903
- if ( this . autoClear ) {
909
+ if ( ( returnedPromise && returnedPromise instanceof Promise ) || ( executionsQty === 0 && hasError ) ) {
904
910
try {
905
- await this . __remove ( task . uid ) ;
906
- this . _debug ( `[FYI] [${ task . uid } ] task was auto-cleared` ) ;
907
- } catch ( removeError ) {
908
- this . _debug ( `[${ task . uid } ] [__execute] [this.autoClear] [__remove] has thrown an exception; removeError:` , removeError ) ;
911
+ await ready ( ) ;
912
+ } catch ( readyErr ) {
913
+ this . _debug ( `[${ task . uid } ] [__execute] [ready] has thrown an exception; readyErr:` , readyErr ) ;
909
914
}
910
- } else if ( this . onError ) {
911
- this . onError ( 'One of your tasks is missing' , {
912
- description : `Something went wrong with one of your tasks - is missing.
913
- Try to use different instances.
914
- It's safe to ignore this message.
915
- If this task is obsolete - simply remove it with \`JoSk#clearTimeout('${ task . uid } ')\`,
916
- or enable autoClear with \`new JoSk({autoClear: true})\`` ,
917
- error : null ,
918
- uid : task . uid
919
- } ) ;
920
- } else {
921
- this . _debug ( `[__execute] [${ task . uid } ] Something went wrong with one of your tasks is missing.
915
+ }
916
+ return ;
917
+ }
918
+
919
+ await this . adapter . update ( task , new Date ( Date . now ( ) + this . zombieTime ) ) ;
920
+ this . tasks [ task . uid ] = function ( ) { } ;
921
+ this . tasks [ task . uid ] . isMissing = true ;
922
+
923
+ if ( this . autoClear ) {
924
+ try {
925
+ await this . __remove ( task . uid ) ;
926
+ this . _debug ( `[FYI] [${ task . uid } ] task was auto-cleared` ) ;
927
+ } catch ( removeError ) {
928
+ this . _debug ( `[${ task . uid } ] [__execute] [this.autoClear] [__remove] has thrown an exception; removeError:` , removeError ) ;
929
+ }
930
+ } else if ( this . onError ) {
931
+ this . onError ( 'One of your tasks is missing' , {
932
+ description : `Something went wrong with one of your tasks - is missing.
922
933
Try to use different instances.
923
934
It's safe to ignore this message.
924
- If this task is obsolete - simply remove it with \`JoSk#clearTimeout(\'${ task . uid } \')\`,
925
- or enable autoClear with \`new JoSk({autoClear: true})\`` ) ;
926
- }
935
+ If this task is obsolete - simply remove it with \`JoSk#clearTimeout('${ task . uid } ')\`,
936
+ or enable autoClear with \`new JoSk({autoClear: true})\`` ,
937
+ error : null ,
938
+ uid : task . uid
939
+ } ) ;
940
+ } else {
941
+ this . _debug ( `[__execute] [${ task . uid } ] Something went wrong with one of your tasks is missing.
942
+ Try to use different instances.
943
+ It's safe to ignore this message.
944
+ If this task is obsolete - simply remove it with \`JoSk#clearTimeout(\'${ task . uid } \')\`,
945
+ or enable autoClear with \`new JoSk({autoClear: true})\`` ) ;
927
946
}
928
947
}
929
948
0 commit comments