Skip to content

Commit 63d792f

Browse files
Fix stack trace not being preserved on timeout (#34)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 67f280c commit 63d792f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export default function pTimeout(promise, options) {
6666
});
6767
}
6868

69+
// We create the error outside of `setTimeout` to preserve the stack trace.
70+
const timeoutError = new TimeoutError();
71+
6972
timer = customTimers.setTimeout.call(undefined, () => {
7073
if (fallback) {
7174
try {
@@ -86,8 +89,8 @@ export default function pTimeout(promise, options) {
8689
} else if (message instanceof Error) {
8790
reject(message);
8891
} else {
89-
const errorMessage = message ?? `Promise timed out after ${milliseconds} milliseconds`;
90-
reject(new TimeoutError(errorMessage));
92+
timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`;
93+
reject(timeoutError);
9194
}
9295
}, milliseconds);
9396

0 commit comments

Comments
 (0)