Home > Back-end >  How to make a reference to a functions with arguments?
How to make a reference to a functions with arguments?

Time:12-15

I am trying to make a reference ep to a function that have arguments. So I have made the below PoC, to illustrate the problem I have run into.

The 3 functions inside selectFunction() all takes different arguments, and in the real world contain axios functions, so that is why I have await.

In the below I get TypeError: ep is not a function.

Question

Can someone figure out what is wrong?

(async () => {
  function getStatusCode(p) {console.log("hi")};
  function getString(p) {console.log("hi")};
  function getSecureString(p) {console.log("hi")};

  const p = {"type": 1, "string": 2, "host": 3};

  function selectFunction(p) {
    switch (p.type) {
      case 1:
      return getStatusCode({
        url: p.url
      });
      break
      case 2:
      return getString({
        string: p.string
      });
      break
      case 3:
      return getSecureString({
        hostname: p.host
      });
      break
      default:
      throw "error";
    };
  };
  // ep should be a function reference
  const ep = selectFunction(p);
  console.log(typeof ep);

  const isOk = await ep();
})();

CodePudding user response:

You’re returning the result of calling the function, not a function itself.

If you need to do this, you might find success in explicitly returning a function reference by wrapping your calls in selectFunction as anonymous functions:

function selectFunction(p) {
  switch (p.type) {
    case 1:
      return function(arg) {
        return getStatusCode({
          url: arg.url
        })
      };
    case 2:
      return function(arg) {
        return getString({
          string: arg.string
        })
      };
    case 3:
      return function(arg) {
        return getSecureString({
          hostname: arg.host
        })
      };
    default:
      throw "error";
  };
};

CodePudding user response:

The below code have the advantage that selectFunction() is only run once.

const delay = require('delay');

(async () => {
  function getStatusCode(p) {console.log("hi1")};
  function getString(p) {console.log("hi2")};
  function getSecureString(p) {console.log("hi3")};

  const p = {"type": 3, "string": 2, "host": 3};

  function selectFunction(p) {
    console.log("only run once");
    switch (p.type) {
      case 1:
      return getStatusCode;
      break
      case 2:
      return getString;
      break
      case 3:
      return getSecureString;
      break
      default:
      throw "error";
    };
  };
  // ep should be a function reference
  const ep = selectFunction(p);
  console.log(typeof ep);

  while (true) {
    const isOk = await ep();
    await delay(1000);
  };
})();

CodePudding user response:

const ep = selectFunction(p) is setting ep to the value returned by selectFunction given p as an argument.

If you want to later use ep(p) and expect it to act like selectFunction(p), this should work:

const ep = p => selectFunction(p);

It is a arrow function.

It does not execute selectFunction, but refers to it.

  • Related