skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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’s executor function.

    Having access to the Proxy constructor’s executer 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.

    // - The actual use case, an asynchronously implemented
    //   `wait` functions which resolves after the provided
    //   amount of milliseconds.
    // - It uses the proxied version of the before created
    //   and reassigned global `Proxy` constructor function.
    
    async function wait(valueInMSec) {
      console.log('... executing ...nn');
    
      return new Promise(resolve =>
        setTimeout(resolve, valueInMSec)
      );
    }
    
    (async () => {
      console.log('waiting ...');
      await wait(3000);
      console.log('n... awaited.');
    })();
    .as-console-wrapper { min-height: 100%!important; top: 0; }
    <script>
    
    // - reassign the `Promise` constructor function
    //   with a proxied version of itself, where the
    //   invocation of its internal `[[Construct]]`
    //   slot gets handled.
    
    window.Promise = (Promise => {
    
      const handlers = {
        construct(target, [executor]) {
    
          console.log(
            ``${ target.name }` creation with following `executer` function ...n`, executor
          );
    
          // - wrap intercepting custom code around the
          //   `Promise` constructor's `executor` function.
          executor = (proceed => {
            return ((/*reject, resolve*/...args) => {
    
              console.log('`executor` intercepted with following args ...n', args);
              console.log('original `executor` function ...n', proceed);
    
              proceed(...args);
            });
          })(executor);
    
          return new target(executor);
        },
      };  
      return new Proxy(Promise, handlers);
    
    })(window.Promise);
    
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search