2

How to find the caller function details inside the promise function

I have referred and implemented the below functionality

How do you find out the caller function in JavaScript?

(async function() {
  try {
    let theFunction = () => {
      return new Promise(async (resolve, reject) => {
        try {
          let CallerDetails = theFunction.caller;
          console.log(CallerDetails);//I cannot fetch the caller details getting error
          resolve("Success")
        } catch (error) {
          console.error(error);
          reject("Something went wrong");
        }
      });
    }
    let theOtherFunction = () => {
      return new Promise(async (resolve, reject) => {
        try {
          let Res = await theFunction();
          resolve(Res);
        } catch (error) {
          console.error(error);
          reject("Something went wrong");
        }
      });
    }

    let Res = await theOtherFunction();
    console.log(Res);
  } catch (err) {
    console.error(err);
  }

}());

I am getting below exception

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
    at Promise (evalmachine.<anonymous>:6:43)
    at new Promise (<anonymous>)
    at theFunction (evalmachine.<anonymous>:4:14)
    at evalmachine.<anonymous>:27:21
    at evalmachine.<anonymous>:33:2
    at Script.runInContext (vm.js:133:20)
    at Object.runInContext (vm.js:311:6)
    at evaluate (/run_dir/repl.js:133:14)
    at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
    at ReadStream.emit (events.js:198:13)
Something went wrong

In my realtime application, I am having error handling common function which is been called in the catch block of more than 200 functions. I am getting an error in common error handling function but not able to locate from where an error has occurred. So for this purpose, I need the caller function.

But in my stack trace, I am getting the dependency(inside node modules) stack and line number are coming. I am unable to locate the actual function. that's why my only hope to rely on knowing the caller function or to add logs on every function which is calling the error handling function

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
  • 2
    `caller` and `callee` are deprecated and should not be used anymore. Try to find logic that does not rely on the caller, worst case, just pass the function name as a parameter. – Shilly Oct 03 '19 at 11:37
  • 1
    What's the exact issue? Why you think you need the caller? – abuduba Oct 03 '19 at 11:38
  • `theFunction` should only rely on `val` to do its calculations. If it needs other information, use more than only `val` as the parameter. But your code shown nothing relying on knowing the caller, so I'm also confused what this is all about. Please explain more. – Shilly Oct 03 '19 at 11:44
  • I am getting an error when accessing the caller function – Ratan Uday Kumar Oct 03 '19 at 11:44
  • You should not access the caller function, it's not standard anymore and can be changed any time browser makers decide to. Why do you think you need the caller function? – Shilly Oct 03 '19 at 11:45
  • In my realtime application, I am having error handling common function which is been called in the catch block of more than 200 functions. I am getting an error in common error handling function but not able to locate from where error has occurred. So for this purpose, I need the caller function – Ratan Uday Kumar Oct 03 '19 at 11:48
  • 1
    Look at the stack trace of the original error? The common error handler should pass the original error to the function that will do the error handling so you have access to the stack and line number and such. I made the same mistake with throwing a `new Error()` in my `catch()` clauses and losing the stack instead of propagating the original error up the error handling pipeline. – Shilly Oct 03 '19 at 11:50
  • But in my stack trace, I am getting the dependency(inside node modules) stack and line number are coming. I am unable to locate the actual function. that's why my only hope to rely on caller function or to add logs on every function which is calling the error handling function – Ratan Uday Kumar Oct 03 '19 at 11:54

0 Answers0