Skip to content

Commit

Permalink
Fix stack trace not being preserved on timeout (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
pius712 and sindresorhus committed Feb 12, 2023
1 parent 67f280c commit 63d792f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -66,6 +66,9 @@ export default function pTimeout(promise, options) {
});
}

// We create the error outside of `setTimeout` to preserve the stack trace.
const timeoutError = new TimeoutError();

timer = customTimers.setTimeout.call(undefined, () => {
if (fallback) {
try {
Expand All @@ -86,8 +89,8 @@ export default function pTimeout(promise, options) {
} else if (message instanceof Error) {
reject(message);
} else {
const errorMessage = message ?? `Promise timed out after ${milliseconds} milliseconds`;
reject(new TimeoutError(errorMessage));
timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`;
reject(timeoutError);
}
}, milliseconds);

Expand Down

0 comments on commit 63d792f

Please sign in to comment.