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 <[email protected]> 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
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss