each
优质
小牛编辑
141浏览
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; }
Name | Type(s) | Description |
---|---|---|
fn | Function | The function to invoke asynchronously on each element in the sequence one by one. |
returns | AsyncHandle | An AsyncHandle providing the ability to cancel the asynchronous iteration (by calling |