javascript - detect end of chained functions?

偶尔善良 提交于 2019-12-31 04:01:08

问题


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.


回答1:


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.




回答2:


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();

;)




回答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'
]);


来源:https://stackoverflow.com/questions/1934247/javascript-detect-end-of-chained-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!