Skip to content

State of the spec ? + useful example #10

@lifaon74

Description

@lifaon74

Hi, I would know where was this project ? I'm very interested in this specs as it would solve many problems js developers face with generator used as "data receiver" instead of "data emiter" (or both).

I may share another very practical use case of function.sent:

export interface IGetPaginatedDataOptions {
  signal?: AbortSignal;
}

export class Pagination<GData> {
  getPaginatedData(page: IPageInfo, options?: IGetPaginatedDataOptions): Promise<IPaginatedData<GData>> {
    return fetch('some url', options).then(_ => _.json());
  }

  /**
   * Creates an async iterator over the list of pages
   */
  async* pageIterator({ pageIndex = 0, itemsPerPage = 10 }: Partial<IPageInfo> = {}): AsyncGenerator<IPaginatedData<GData>, any, any> {
    const page: IPageInfo = { pageIndex, itemsPerPage };
    let result: IPaginatedData<GData>;
    do {
      result = await this.getPaginatedData(page, function.sent); // note just here the usage of funtion.sent
      yield result;
      page.pageIndex++;
    } while (result.pageCount > page.pageIndex);
  }
}

const pagination = new Pagination().pageIterator();
const controller = new AbortController();
setTimeout(() => controller.abort('timeout'), 5000);
const page = await pagination.next(controller);

It could be extremely useful for AsyncIterators ! Hope it's not dead ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions