@async-generators/terminator

中文文档
0.4.0发布于 2018/01/16MITTypeScriptESM + CJS零依赖近 30 天下载 2738

inform an iterator when it is prematurely terminated by the consumer.

原始使用文档
文档目录5 节
  1. Install
  2. Api
  3. terminator(source)
  4. Example
  5. Typescript

terminator

logo

inform an iterable when it is prematurely terminated by the consumer.

NPM version Travis Status Travis Status

Install

yarn add @async-generators/terminator

This package's main entry points to a commonjs dist. The module entry points to a es2015 module dist. Both require require native async-generator support, or be down-compiled with a webpack loader.

Api

terminator(source)

terminator() returns an iterable that, when iterated, wraps the source iterable and captures calls to return. When a compliant consumer (for, for-await) terminates iteration, then the return call is rerouted to call next(Symbol.for("terminated")). The original source iterable can then find out if it was terminated by examining the return value of yield to see if it equals Symbol.for("terminated");

source must have a [Symbol.asyncIterator] or [Symbol.iterator] property. If both are present only [Symbol.asyncIterator] is used.

Example

example.js

const terminator = require("@async-generators/terminator").default;

async function* source() {
  for (let i = 0; i < 10; i++) {
    let terminated = (yield i) == Symbol.for("terminated");
    if (terminated) {
      console.log("clean-up on aisle five!");
      return;
    }
  }
}

async function main(){
  for await (let item of terminator(source())){
    if(item > 4){
      break;
    }
    console.log(item);
  }
}

main().catch(console.log);

Execute with the latest node.js:

node --harmony-async-iteration example.js

output:

0
1
2
3
4
clean-up on aisle five!

Typescript

This library is fully typed and can be imported using:

import terminator from '@async-generators/terminator');

It is also possible to directly execute your properly configured typescript with ts-node:

ts-node --harmony_async_iteration example.ts

版本与发布记录

@async-generators/terminator 共发布 6 个版本,首次发布于 2017/09/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.0latest2018/01/168 年前
0.3.12018/01/138 年前
0.3.02018/01/128 年前
0.2.02017/09/269 年前
0.1.12017/09/269 年前
0.1.02017/09/269 年前

关于 @async-generators/terminator 的常见问题

@async-generators/terminator 是什么?

inform an iterator when it is prematurely terminated by the consumer.

如何安装 @async-generators/terminator?

在项目目录运行 npm install @async-generators/terminator,或使用 pnpm add / yarn add / bun add。

@async-generators/terminator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@async-generators/terminator 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@async-generators/terminator 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=9.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@async-generators/terminator 可以商用吗?

@async-generators/terminator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@async-generators/terminator 的下载量有多少?

根据 npm 官方下载统计,@async-generators/terminator 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,738 次,日均约 91 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@async-generators/terminator 还在维护吗?

最新版本 0.4.0 发布于 2018/01/16(8 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。