is it possible to retrieve the code from a Promise Object? Suppose I set up a Promise like this:
let myPromise = new Promise(function(success, error) {
...
}
Can I somehow retrieve the anonymous function or the location of the anonymous function in the code file?
function(success, error) {
...
}
from myPromise?
2
Answers
No. The promise object does not contain the code, neither internally nor accessible to JavaScript. A promise represents the result of a task, not the task itself, it is not runnable. The function is used only during construction.
The next provided example code, for demonstration purposes, stretches an overwriting and wrapping introspection approach as far as one can go.
One has to do two things …
reassign the
Promise
constructor function with a proxied version of itself, where the invocation of its internal[[Construct]]
slot gets handled by custom code.wrap intercepting custom code around the
Promise
constructor’sexecutor
function.Having access to the
Proxy
constructor’sexecuter
function and logging it might reveal some of its implementation details, thus being possibly able to provide some hints about where to drill down next.