views:

141

answers:

3

Today I'm working on a pet project using chained function calls, and I'm curious how I might detect when the last function in the chain is executed. For example:

func1('initial data').func2().func3().func4();

And after func2-4 have finished working on 'initial data' I'd like to detect when func4 is done. Since func4() isn't always the last function in the chain, aka it could end at .func3() or .func5() for example, or I could mix my function calls up depending on what I'm trying to do, I'm trying to think of a way to detect no more function calls are being done but I'm not getting very far.

+3  A: 

The traditional approach is to put whatever you want done after the final function on the next line:

func1('initial data').func2().func3().func4();
allFunctionsDone();

;)

Rich
Exactly. There is noting strange or mysterious about issue. When JavaScript is executed synchronously, it behaves like any other procedural language. When the function returns, its done, and then you can carry on.
Justin Johnson
+2  A: 

You can write the sequencer, which will help you to do this for you. Instead of executing direct calls, shift the names of the functions and call them one by one. Something like this

executeSequence(func1('init_dat'),[
    'func2',
    'func3',
    'func4'
]);
nemisj
There is zero benefit to this.
Justin Johnson
I voted this up because there is a benefit if one of the objects returned might or might not have the next method in the chain. The sequencer can check whether the next method exists before invoking it.
Rich
@Justin Johnoson, IMHO: It completely depends how and in which context you use this code. If you build some dynamic code which can be parameterized, with the nested sequencers, with error handling, start() and stop() procedures, etc, it can be quite useful.
nemisj
Let me rephrase that then, there is zero benefit to this in relation to the actual question. While start and stop procedures, etc are useful and necessary in some cases, this wasn't the question and the logic to support this is way more than necessary. In fact, no additional logic is necessary as the OP's question is really very simple as answered by @Rich.
Justin Johnson
+3  A: 

You can't.

Besides, if they are not chained:

var v = func1('initial data');
v = v.func2();
v = v.func3();
v = v.func4();

What would you consider to be the last function? Every function is the last function in it's own chain, but if you finalise something after each step, that won't work.

Just make a function that you call last to finalise the process.

Guffa
Yeah this is basically what I'd already done. I had created a done() function so I would chain like func1().func2().func3().done();It works well enough for the time being.
Geuis
Sure you can. When the function returns, then the function is complete.
Justin Johnson
@Justin: You misunderstood the question. He doesn't want to know when EACH function is complete, he want's to know when THE LAST function is complete.
Guffa
@Guffa I do not misunderstand. I too was under the impression that he wanted to know when the last function is complete. See @Rich's answer.
Justin Johnson
@Justin: You are still missing the point completely. He DIDN'T WANT to make that extra call afterwards, but detect it from within the functions.
Guffa