-1

Here is a simplified version of my code:

function caller( _func_to_call , param){

       console.log( ` @@@>  CALLING  ${_func_to_call.toString()} `); 
       _func_to_call(param);
}

In this line:

 console.log( ` @@@>  CALLING  ${_func_to_call.toString()} `); 

I want to output the name of the function that its going to be called, but it's logging all the function with its content, like this:

 @@@>  CALLING  async function start_transaction(transaction){

    update_transaction_date(transaction);

    var tab_id = `transaction-${transaction.id}`;
    console.log(`----> START TRANSACTION ${tab_id}`);
    ...

How can I get it to only output start_transaction?

Marco Bonelli
  • 55,971
  • 20
  • 106
  • 115
hretic
  • 676
  • 7
  • 30
  • 60
  • 3
    This question could be ommited by just googling "function name js" – Konrad Linkowski Nov 17 '18 at 18:24
  • Possible duplicate of [How to get the function name from within that function?](https://stackoverflow.com/questions/2648293/how-to-get-the-function-name-from-within-that-function) – vrintle Nov 17 '18 at 18:29

3 Answers3

4

Take a look at Function.prototype.name. You can use it like this:

console.log(` @@@>  CALLING  ${_func_to_call.name} `); 

Now since you're logging always the same function name it doesn't make much sense, but in general you can log any function name using the .name attribute. Here's an example:

function logFuncName(func) {
    console.log(func.name);
}

logFuncName(function foo() {});

Output:

foo
Marco Bonelli
  • 55,971
  • 20
  • 106
  • 115
3

To get a function's name you have to get the name property, so in your case _func_to_call.name.

Function.prototype.name

Denis Frezzato
  • 842
  • 5
  • 13
0

There is a function property called name. _func_to_call.name

Konrad Linkowski
  • 2,351
  • 1
  • 20
  • 33