Any way to detect an async stack trace (like Chrome devtools does)?
kai zhu
kaizhu256 at gmail.com
Wed Jul 8 09:13:45 UTC 2020
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/20200708/af4a3a54/attachment.html>
More information about the es-discuss
mailing list