返回介绍

AsyncHandle

发布于 2019-05-31 13:13:13 字数 2022 浏览 1087 评论 0 收藏 0

An AsyncHandle provides a Promises/A+ compliant interface for an AsyncSequence that is currently (or was) iterating over its elements.

In addition to behaving as a promise, an AsyncHandle provides the ability to AsyncHandle#cancel iteration (if cancelFn is provided) and also offers convenient AsyncHandle#onComplete and AsyncHandle#onError methods to attach listeners for when iteration is complete or an error is thrown during iteration.

Signature

function AsyncHandle(cancelFn) { /*...*/ }
function AsyncHandle(cancelFn) {
  this.resolveListeners = [];
  this.rejectListeners = [];
  this.state = PENDING;
  this.cancelFn = cancelFn;
}
NameType(s)Description
cancelFnFunction

A function to cancel asynchronous iteration. This is passed in to support different cancellation mechanisms for different forms of asynchronous sequences (e.g., timeout-based sequences, sequences based on I/O, etc.).

Examples

// Create a sequence of 100,000 random numbers, in chunks of 100.
var sequence = Lazy.generate(Math.random)
  .chunk(100)
  .async()
  .take(1000);

// Reduce-style operations -- i.e., operations that return a *value* (as
// opposed to a *sequence*) -- return an AsyncHandle for async sequences.
var handle = sequence.toArray();

handle.onComplete(function(array) {
  // Do something w/ 1,000-element array.
});

// Since an AsyncHandle is a promise, you can also use it to create
// subsequent promises using `then` (see the Promises/A+ spec for more
// info).
var flattened = handle.then(function(array) {
  return Lazy(array).flatten();
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文