Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pass parameters to a function without invoking it right away?

I need to feed a pipe() handler function a bunch of function names so it can execute them in order, waiting for completion of each as it goes. This is great when those functions don't need parameters passing, but when parameters are needed I can't figure out how to pass them without the function going ahead and invoking itself (caused by the brackets).

For example, this is what I typically pass:

pipeHandler([function1, function2]);

It'll then invoke function1() and function2() before the promise is completed.

Where it gets difficult is when I want to do something like thiss:

pipeHandler([function1('value'), function2]);

That causes function1() to invoke immediately, completely bypassing the promise mechanism.

In case it helps, this is the handler function:

function pipeHandler(requiredFunctions) {
    //Execute first required function
    var executeFunctions = requiredFunctions[0]();

    //Execute each subsequent required function using pipe()
    for ( var index = 1; index < requiredFunctions.length; index++ ) {
        executeFunctions = executeFunctions.pipe(requiredFunctions[index]);
    }

    //Execute allDone() using pipe()
    executeFunctions = executeFunctions.pipe(allDone);
}

Hope somebody has an idea!

like image 336
Ryan Williams Avatar asked Feb 04 '14 10:02

Ryan Williams


People also ask

How the parameters can be passed to a function?

Call by reference When the function is called, the parameter passed to it must be a variable, and that variable's address is passed to the function. Any time the function's body uses the parameter, it uses the variable at the address that was passed. For example, suppose that inc is defined as follows.

When a parameter is passed to a function it is called?

The most common evaluation strategy when passing arguments to a function has been call by value and call by reference: call by value. The most common strategy is the call-by-value evaluation, sometimes also called pass-by-value.

Is it necessary to pass all the arguments to call a function?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.


1 Answers

Why not

pipeHandler([function() { function1('value'); }, function2]);

?

This is where anonymous functions shine. If you spend some time working in Javascript, you'll probably encounter the same problem when using setTimeOut at some point.

like image 50
Moeri Avatar answered Sep 24 '22 00:09

Moeri