Any way to detect an async stack trace (like Chrome devtools does)?

#!/JoePea joe at trusktr.io
Fri Jul 10 08:34:20 UTC 2020


Thanks for the idea! That's similar to what I thought would be necessary. I
was hoping to monkey patch `fetch()` to have it get the trace if any
`fetch` call, but they would not be async stack traces. For async traces it
would require instrumentation by injecting code similar to yours (I want to
detect traces in third-party code installed locally).

#!/JoePea

On Wed, Jul 8, 2020, 2:13 AM kai zhu <kaizhu256 at gmail.com> wrote:

> here's a simple throwaway-function you can wrap around promises like fetch
> to get the caller's async-stack-trace `promiseWithErrorStack`:
>
> ```html
> <!doctype html>
> <html lang="en">
> <body>
> <h1>test.html</h1>
> <script>
> (async function foo() {
>     function promiseWithErrorStack(promise) {
>     /*
>      * this function will append current-stack to any err caught from
> <promise>
>      */
>         let errStack;
>         errStack = new Error().stack;
>         return new Promise(function (resolve, reject) {
>             promise.then(resolve).catch(function (err) {
>                 // append current errStack to err.stack
>                 if (err && typeof err.stack === "string") {
>                     err.stack += "\n" + errStack;
>                 }
>                 reject(err);
>             });
>         });
>     }
>     await promiseWithErrorStack(fetch("https://example.com")); // at foo
> (test.html:23)
>
>     /*
>     console-output:
>
>     Uncaught (in promise) TypeError: Failed to fetch
>     Error
>         at promiseWithErrorStack (test.html:12)
>         at foo (test.html:23) // async-stack-trace
>         at test.html:32
>     */
> }());
> </script>
> </body>
> ```
>
> On Tue, Jul 7, 2020 at 11:54 PM #!/JoePea <joe at trusktr.io> wrote:
>
>> Is there some way (perhaps by patching methods on objects?) so we can
>> track async call stacks?
>>
>> When we pause code in devtools, we are able to see the async stack
>> trace of the code.
>>
>> What I'd like to do is to effectively detect the same thing as
>> devtools does at some point in any code. As a specific example, I'd
>> like to detect the async stack trace of any call to `fetch`.
>>
>> Is such a thing possible with runtime code?
>>
>> Or would it require instrumentation of the source code?
>>
>> #!/JoePea
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200710/7091ab73/attachment.html>


More information about the es-discuss mailing list