te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to yield* arrays in Typescript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to yield* arrays in Typescript - Stack Overflow

programmeradmin4浏览0评论

I have a generator method that adds asyncronously some stuff to an array and at the end yields each member with yield* array like so:

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

However our friendly neighbor tsc reports an error and I can't even begin to understand it or how to fix it. Can anybody explain what does this error mean and how am I supposed then to yield the contents of an array?

TS2766: Cannot delegate iteration to value because the 'next' method of its iterator expects type 'undefined', but the containing generator will always send 'unknown'.

Edit: the whole generator function follows

  /**
   * 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);
  }

I have a generator method that adds asyncronously some stuff to an array and at the end yields each member with yield* array like so:

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

However our friendly neighbor tsc reports an error and I can't even begin to understand it or how to fix it. Can anybody explain what does this error mean and how am I supposed then to yield the contents of an array?

TS2766: Cannot delegate iteration to value because the 'next' method of its iterator expects type 'undefined', but the containing generator will always send 'unknown'.

Edit: the whole generator function follows

  /**
   * 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);
  }
Share Improve this question edited Apr 14, 2020 at 12:51 dorphalsig asked Apr 13, 2020 at 22:26 dorphalsigdorphalsig 7111 gold badge10 silver badges30 bronze badges 6
  • Did you really mean to use yield* or do you need a normal yield? – VLAZ Commented Apr 13, 2020 at 22:29
  • @VLAZ I really mean yield* I want to yield each member of the array individually – dorphalsig Commented Apr 13, 2020 at 22:31
  • 1 Maybe related: stackoverflow./questions/58568399/… – Mark Commented Apr 13, 2020 at 22:36
  • More context is needed (mainly the function* definition). – Jonas Wilms Commented Apr 13, 2020 at 22:38
  • The problem is that .next(stuff) will pass a value into the iterator, and as you yield* also into the array iterator. The array iterator is typed as Iterator<void, T, undefined> so you may only call it as .next(), however your generator function is typed differently (or not at all). Therefore the .next(...) calls clash. – Jonas Wilms Commented Apr 13, 2020 at 22:39
 |  Show 1 more ment

1 Answer 1

Reset to default 15

Your main Problem is AsyncGenerator<Event> ... As you did not specify the third generic parameter, it's default value (unknown) is used, and unknown missmatches undefined.

The missmatch occurs because the inner iterator does not expect any values passed into the iterator via next(value), therefore it's TNext generic parameter is undefined. As you yield* that iterator, calling next(...) on your generators iterator will call the inner iterator with the same arguments, therefore the arguments have to match.

Typescript infers the type of a generator function to AsyncGenerator<..., ..., undefined> for exactly that reason, and you should do that too (as long as you dont want to pass values into the iterator with .next): AsyncGenerator<Event, void, undefined>.

发布评论

评论列表(0)

  1. 暂无评论