@@ -8,11 +8,13 @@ export function timeout<R>(
8
8
updateIntervalMs : number ,
9
9
maxDurationMs : number ,
10
10
action : ( ...params : any ) => Promise < R > ,
11
- config ?: TimoutConfig
11
+ config ?: TimoutConfig ,
12
12
) : Promise < R > {
13
13
return new Promise < R > ( ( resolve , reject ) => {
14
14
let interval : NodeJS . Timeout ;
15
15
let timerCleaned = false ;
16
+ let lastResult : R | null ;
17
+ let lastRejectionReason : any | null ;
16
18
17
19
if ( config ?. signal ) {
18
20
config . signal . onabort = ( ) => {
@@ -29,12 +31,16 @@ export function timeout<R>(
29
31
if ( ! result && ! timerCleaned ) {
30
32
interval = setTimeout ( executeInterval , updateIntervalMs ) ;
31
33
} else {
34
+ lastResult = result ;
35
+ lastRejectionReason = null ;
32
36
cleanupTimer ( ) ;
33
37
resolve ( result ) ;
34
38
}
35
39
}
36
40
37
- function handleRejection ( ) {
41
+ function handleRejection ( reason : any ) {
42
+ lastRejectionReason = reason ;
43
+ lastResult = null ;
38
44
if ( ! timerCleaned ) {
39
45
interval = setTimeout ( executeInterval , updateIntervalMs ) ;
40
46
}
@@ -52,7 +58,17 @@ export function timeout<R>(
52
58
53
59
const maxTimeout = setTimeout ( ( ) => {
54
60
cleanupTimer ( ) ;
55
- reject ( `Action timed out after ${ maxDurationMs } ms` ) ;
61
+ let additionalInformation : string | undefined ;
62
+ if ( lastResult == null && lastRejectionReason != null ) {
63
+ additionalInformation = `Last rejection reason was: ${ lastRejectionReason } .` ;
64
+ } else if ( lastResult == null && lastRejectionReason == null ) {
65
+ additionalInformation = `Didn't receive a result within timeout.` ;
66
+ }
67
+ reject (
68
+ `Action timed out after ${ maxDurationMs } ms.${
69
+ additionalInformation ? ` ${ additionalInformation } ` : ""
70
+ } `,
71
+ ) ;
56
72
} , maxDurationMs ) ;
57
73
58
74
executeInterval ( ) ;
0 commit comments