Ilja Ilja - 5 months ago 12
Javascript Question

Pass function with parameters, but not its execution result

In my component I have a static

need
property that stores a
function
. Behind the scenes there is middleware that executes this
function
in order to perform some asynchronous API calls before rendering the component. This is how it looks:

static need = [
myFunc(token)
]


However this causes issues, as having a
token
parameter there actually executes the function, hence its promise is passed as
need
, instead of
function
itself. For example, this works perfectly fine, as it doesn't execute the function:

static need = [
myFunc
]


But this way I have no ability to pass a token. I'm trying to figure out how to pass this function to
need
with a token, but without executing it, as it is done in middleware.

Answer

You could use a closure

static need = [
    function (token) {
        return myFunc;
    }(token);
]

ES6

static need = [
    (token => myFunc)(token)
]
Comments