终结/后处理链式方法
假设我有一个类似数组的对象,它允许像这样链接方法:
var ds = new DataSet(items);
var subset = ds.filter(condition1).filter(condition2);
有没有办法在处理链中的最后一个方法后执行代码 - 而无需手动附加单独的 .finalize()
(或 end
/execute
/...)方法调用?
我相信我以前在某处见过这个,但我不记得细节了。也许 futures/promises/deferreds 在这里有用 - 但我真的不知道如何使用,因为我总是最终需要“展望未来”来确定方法调用是否是链中的最后一个。
Assuming I have an Array-like object which allows for chaining methods like so:
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你控制
filter
方法,添加一个finalize参数可能是一个想法?in
filter
:[edit based on comment]
另一种方法可能是允许终结器条件,因此在
.filter(condition)
中,condition
的内容定义了最终确定的需要。比如(我不知道你的病情类型,只是编造的):
If you control the
filter
method, it may be an idea to add a finalize parameter?in
filter
:[edit based on comment]
Another approach could be to allow for a finalizer condition, so in
.filter(condition)
, the contents ofcondition
defines the need to finalize.Something like (I don't know the type of your condition, just making things up):