目录
当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

each

优质
小牛编辑
134浏览
2023-12-01

An asynchronous version of Sequence#each.

Signature

AsyncSequence.each = function(fn) { /*...*/ }
AsyncSequence.each = function each(fn) {
  var iterator = this.parent.getIterator(),
  onNextCallback = this.onNextCallback,
  cancelCallback = this.cancelCallback,
  i = 0;

  var handle = new AsyncHandle(function cancel() {
if (cancellationId) {
  cancelCallback(cancellationId);
}
  });

  var cancellationId = onNextCallback(function iterate() {
cancellationId = null;

try {
  if (iterator.moveNext() && fn(iterator.current(), i++) !== false) {
    cancellationId = onNextCallback(iterate);

  } else {
    handle._resolve();
  }

} catch (e) {
  handle._reject(e);
}
  });

  return handle;
}
NameType(s)Description
fnFunction

The function to invoke asynchronously on each element in the sequence one by one.

returnsAsyncHandle

An AsyncHandle providing the ability to cancel the asynchronous iteration (by calling cancel()) as well as supply callback(s) for when an error is encountered (onError) or when iteration is complete (onComplete).