1

假设我有一个类似数组的对象,它允许像这样链接方法:

var ds = new DataSet(items);
var subset = ds.filter(condition1).filter(condition2);

有没有办法在处理完链中的最后一个方法后执行代码- 无需手动附加单独的.finalize()(或end// execute...)方法调用?

我相信我以前在某个地方见过这个,但我不记得细节了。也许 futures/promises/deferreds 在这里会用到——但我真的不知道怎么用,因为我总是需要“展望未来”来确定方法调用是否是链中的最后一个。

4

1 回答 1

1

If you control the filter method, it may be an idea to add a finalize parameter?

.filter(condition,[some finalizer function]);

in 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.

Something like (I don't know the type of your condition, just making things up):

if (condition.match('f=true')) {
  finalizer();
}
于 2011-04-16T07:43:41.310 回答