File tree Expand file tree Collapse file tree 3 files changed +28
-0
lines changed Expand file tree Collapse file tree 3 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ export type RequestOptions = {
48
48
49
49
/**
50
50
* Can be used to cancel an in-flight request. This will cause an AbortError to be raised from request().
51
+ *
52
+ * Use abortAfterTimeout() to easily implement timeouts using this signal.
51
53
*/
52
54
signal ?: AbortSignal ;
53
55
} ;
Original file line number Diff line number Diff line change
1
+ import { abortAfterTimeout } from "./utils.js" ;
2
+
3
+ describe ( "abortAfterTimeout" , ( ) => {
4
+ it ( "should abort after timeout" , ( ) => {
5
+ const signal = abortAfterTimeout ( 0 ) ;
6
+ expect ( signal . aborted ) . toBe ( false ) ;
7
+
8
+ return new Promise ( ( resolve ) => {
9
+ setTimeout ( ( ) => {
10
+ expect ( signal . aborted ) . toBe ( true ) ;
11
+ resolve ( true ) ;
12
+ } , 0 ) ;
13
+ } ) ;
14
+ } ) ;
15
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Returns an AbortSignal that will enter aborted state after `timeoutMs` milliseconds.
3
+ */
4
+ export function abortAfterTimeout ( timeoutMs : number ) : AbortSignal {
5
+ const controller = new AbortController ( ) ;
6
+ setTimeout ( ( ) => {
7
+ controller . abort ( ) ;
8
+ } , timeoutMs ) ;
9
+
10
+ return controller . signal ;
11
+ }
You can’t perform that action at this time.
0 commit comments