Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Typescript中生成*数组_Javascript_Arrays_Typescript_Async Await_Generator - Fatal编程技术网

Javascript 如何在Typescript中生成*数组

Javascript 如何在Typescript中生成*数组,javascript,arrays,typescript,async-await,generator,Javascript,Arrays,Typescript,Async Await,Generator,我有一个生成器方法,它异步地向数组中添加一些内容,最后生成每个成员的yield*array,如下所示: while (!done) { await promise; yield* results; //<-- the array results = []; //its emptied so as to not to yield dupes done = !this.#eventsListened.has(eventName); }

我有一个生成器方法,它异步地向数组中添加一些内容,最后生成每个成员的
yield*array
,如下所示:

while (!done) {
      await promise;
      yield* results; //<-- the array
      results = [];  //its emptied so as to not to yield dupes
      done = !this.#eventsListened.has(eventName);
    }
while(!done){
等待承诺;

yield*results;//您的主要问题是
AsyncGenerator
…因为您没有指定第三个通用参数,所以使用了它的默认值(
unknown
),并且
unknown
未定义的
不匹配

发生错误匹配是因为内部迭代器不希望任何值通过
next(value)
传递到迭代器中,因此它的
TNext
泛型参数是
undefined
在生成器上,迭代器将使用相同的参数调用内部迭代器,因此参数必须匹配


Typescript正是基于这个原因将生成器函数的类型推断为
AsyncGenerator
,您也应该这样做(只要您不想使用
.next
将值传递到迭代器中):
AsyncGenerator

你是真的想使用还是需要一个正常的
yield
?@VLAZ我真的是指yield*我想让数组中的每个成员都独立起来可能相关:需要更多的上下文(主要是
函数*
定义)。问题是
。下一步(stuff)
将向迭代器传递一个值,当您
产生*
时,也会向数组迭代器传递一个值。数组迭代器的类型为
迭代器
,因此您只能将其称为
.next()
,但是您的生成器函数的类型不同(或者根本没有)。因此,
.next(…)
调用clash。谢谢!这就成功了……我不知道AsyncGenerator接口有三种类型。我仍然很困惑,您是否可以向迭代器发送一个值。
  /**
   * Returns an iterator that loops over caught events
   * @yields Promise<Event|CustomEvent>
   */
  async *on(eventName: string, options?: AddEventListenerOptions): AsyncGenerator<Event> {
    if (this.#eventsListened.has(eventName)) {
      return;
    }
    let results: Event[] = [];

    let resolve: (value?: PromiseLike<Event> | Event) => void;

    let promise = new Promise<Event>(r => (resolve = r));
    let done = false;
    this.#eventsListened.add(eventName);

    if (typeof options === 'object' && typeof options.once !== 'undefined') {
      throw new Error('options.once is not supported. Use EventTarget2.once instead');
    }
    const callback = (evt: Event) => {
      results.push(evt);
      resolve();
      promise = new Promise<Event>(r => (resolve = r));
    };

    this.addEventListener('eventName', callback, options);
    while (!done) {
      await promise;
      yield* results;
      results = [];
      done = !this.#eventsListened.has(eventName);
    }
    this.removeEventListener(eventName, callback);
  }