[英]finalizing / post-processing chained methods
Assuming I have an Array-like object which allows for chaining methods like so: 假设我有一个类似Array的对象,它允许像这样的链接方法:
var ds = new DataSet(items);
var subset = ds.filter(condition1).filter(condition2);
Is there a way to execute code after the last method in the chain has been processed - without resorting to manually appending a separate .finalize()
(or end
/ execute
/...) method call? 在处理完链中的最后一个方法之后 ,是否有一种方法可以执行代码-无需手动附加一个单独的
.finalize()
(或end
/ execute
/ ...)方法调用?
I believe I've seen this before somewhere, but I don't recall the details. 我相信我以前在某处已经看到过这个,但是我不记得细节了。 Perhaps futures/promises/deferreds would be of use here - but I don't really know how, since I always end up with the need to "look into the future" to determine whether a method call is the last in the chain.
也许在这里会使用期货/承诺/递延期权-但我真的不知道怎么做,因为我总是以“展望未来”来确定方法调用是否是链中的最后一个。
If you control the filter
method, it may be an idea to add a finalize parameter? 如果您控制
filter
方法,可能会添加一个finalize参数?
.filter(condition,[some finalizer function]);
in filter
: 在
filter
:
if (finalizer){
finalizer();
}
[ edit based on comment ] [ 根据评论编辑 ]
Another approach could be to allow for a finalizer condition, so in .filter(condition)
, the contents of condition
defines the need to finalize. 另一种方法可能是允许一个终结条件,所以在
.filter(condition)
,内容condition
确定最终确定的需要。
Something like (I don't know the type of your condition, just making things up): 有点像(我不知道您的病情类型,只是弥补问题):
if (condition.match('f=true')) {
finalizer();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.