1

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.

Geuis
  • 41,122
  • 56
  • 157
  • 219
  • If the functions are synchronous, then there is nothing special about this. A function is done when it returns, regardless of whether its chained nor not. – Justin Johnson Dec 20 '09 at 03:52

3 Answers3

5

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
  • 687,336
  • 108
  • 737
  • 1,005
  • 2
    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 Dec 19 '09 at 22:08
  • Sure you can. When the function returns, then the function is complete. – Justin Johnson Dec 20 '09 at 03:54
  • @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 Dec 20 '09 at 10:51
  • @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 Dec 20 '09 at 20:58
  • 1
    @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 Dec 20 '09 at 22:43
3

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
  • 3,095
  • 17
  • 17
  • 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 Dec 20 '09 at 03:56
3

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
  • 11,562
  • 2
  • 25
  • 23
  • There is zero benefit to this. – Justin Johnson Dec 20 '09 at 03:55
  • 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 Dec 20 '09 at 09:23
  • @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 Dec 20 '09 at 11:20
  • 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 Dec 20 '09 at 20:56