ES2018 Async Generator 的简明部分应用?
Concise partial application of ES2018 Async Generator?
我定义了一个异步生成器函数,我想创建第二个异步生成器函数,它是第一个函数的部分应用程序。例如,这有效:
async function* fn1(a, b) {/* do something */}
async function* fn2() {
const a1 = fn1(0, 0);
for await (const value of a1) {
yield value;
}
}
我的问题是:有没有更简洁的方式来定义fn2
?
fn1
returns fn2
正在迭代的异步可迭代对象,因此与其在 fn2
中手动迭代,不如直接生成从 fn1
并让 fn2
的消费者处理它:
async function* fn2() {
yield* fn1(0, 0);
}
const delay = ms => new Promise(res => setTimeout(res, ms));
async function* fn1(a, b) {
for (let i = 0; i < 3; i++) {
await delay(1000);
yield i;
}
}
async function* fn2() {
yield* fn1(0, 0);
}
(async () => {
for await (const val of fn2()) {
console.log('Consumer got', val);
}
console.log('Done');
})();
// Original version, without yield* (just to illustrate equivalence):
const delay = ms => new Promise(res => setTimeout(res, ms));
async function* fn1(a, b) {
for (let i = 0; i < 3; i++) {
await delay(1000);
yield i;
}
}
async function* fn2() {
const a1 = fn1(0, 0);
for await (const value of a1) {
yield value;
}
}
(async () => {
for await (const val of fn2()) {
console.log('Consumer got', val);
}
console.log('Done');
})();
最简洁的定义可以通过简单地不使 fn2
成为生成器函数来实现。使用 returns 生成器的普通函数 - 通过调用 fn1
:
创建的生成器
function fn2() {
return fn1(0, 0);
}
我定义了一个异步生成器函数,我想创建第二个异步生成器函数,它是第一个函数的部分应用程序。例如,这有效:
async function* fn1(a, b) {/* do something */}
async function* fn2() {
const a1 = fn1(0, 0);
for await (const value of a1) {
yield value;
}
}
我的问题是:有没有更简洁的方式来定义fn2
?
fn1
returns fn2
正在迭代的异步可迭代对象,因此与其在 fn2
中手动迭代,不如直接生成从 fn1
并让 fn2
的消费者处理它:
async function* fn2() {
yield* fn1(0, 0);
}
const delay = ms => new Promise(res => setTimeout(res, ms));
async function* fn1(a, b) {
for (let i = 0; i < 3; i++) {
await delay(1000);
yield i;
}
}
async function* fn2() {
yield* fn1(0, 0);
}
(async () => {
for await (const val of fn2()) {
console.log('Consumer got', val);
}
console.log('Done');
})();
// Original version, without yield* (just to illustrate equivalence):
const delay = ms => new Promise(res => setTimeout(res, ms));
async function* fn1(a, b) {
for (let i = 0; i < 3; i++) {
await delay(1000);
yield i;
}
}
async function* fn2() {
const a1 = fn1(0, 0);
for await (const value of a1) {
yield value;
}
}
(async () => {
for await (const val of fn2()) {
console.log('Consumer got', val);
}
console.log('Done');
})();
最简洁的定义可以通过简单地不使 fn2
成为生成器函数来实现。使用 returns 生成器的普通函数 - 通过调用 fn1
:
function fn2() {
return fn1(0, 0);
}