Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve error stack trace in timer #34

Merged
merged 4 commits into from Feb 12, 2023

Conversation

pius712
Copy link
Contributor

@pius712 pius712 commented Feb 10, 2023

when error occurred that made in callback function in timer,
error stack is missing.
So to prevent from missing error stack, I declared error from outer of timer callback.

@sindresorhus
Copy link
Owner

Can you show the stack trace before and after this change?

@pius712
Copy link
Contributor Author

pius712 commented Feb 12, 2023

If run code below.

async function foo() {
    await bar();
}

async function bar() {
    await pTimeout(sleep(50), {milliseconds: 10})
}

async function sleep (ms: number) {
    return new Promise((res)=> {
        setTimeout(()=>{
            res('hi')
        }, ms)})
}
(async ()=>{
        await foo();
    })()

fixed version

TimeoutError: Promise timed out after 10 milliseconds
    at file:///myPath/node_modules/p-timeout/index.js:68:24
    at new Promise (<anonymous>)
    at pTimeout (file:///myPath/node_modules/p-timeout/index.js:48:28)
    at file:///myPath/src/index.ts:11:11
    at Generator.next (<anonymous>)
    at file:///myPath/src/index.ts:7:71
    at new Promise (<anonymous>)
    at __awaiter (file:///myPath/src/index.ts:3:12)
    at bar (file:///myPath/src/index.ts:17:12)
    at file:///myPath/src/index.ts:6:11

prev version shows

TimeoutError: Promise timed out after 10 milliseconds
    at Timeout._onTimeout (file:///MyPath/node_modules/p-timeout/index.js:90:12)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)

@pius712
Copy link
Contributor Author

pius712 commented Feb 12, 2023

prev version doesn't show any information about caller, because they made in timer callback function that saved in timers queue in terms of event loop. But fixed version can show it because error is made before timer, so capture trace.

@sindresorhus sindresorhus merged commit 63d792f into sindresorhus:main Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants