-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State of the spec ? + useful example #10
Comments
why couldn't you just pass that into the iterator? 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(controller, { 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, controller);
yield result;
page.pageIndex++;
} while (result.pageCount > page.pageIndex);
}
}
const controller = new AbortController();
const pagination = new Pagination().pageIterator(controller);
setTimeout(() => controller.abort('timeout'), 5000);
const page = await pagination.next(); |
In my example, the purpose is to cancel individually the request, not the whole iteration |
@lifaon74 Thank you for the wonderful example. I'm planning update the proposal in next TC39 meeting. My idea is moving from ...
/**
* Creates an async iterator over the list of pages
*/
async* pageIterator({ pageIndex = 0, itemsPerPage = 10 } = {})
receive (signal: AbortSignal) {
const page = { pageIndex, itemsPerPage };
let result: IPaginatedData<GData>;
do {
result = await this.getPaginatedData(page, {signal});
yield result;
page.pageIndex++;
} while (result.pageCount > page.pageIndex);
}
...
const page = await pagination.next(controller.signal) // only send signal to avoid exposing too many power (like .abort()) I think this example also show some merits of receive param than First, the information is on the method signature now. In the Second, even we see Third, it also allow TS infer the return type of the generator function so I removed the wordy return type (See #13 ). I hope this direction could be approved by the committee, and help your use cases :-) |
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
:It could be extremely useful for AsyncIterators ! Hope it's not dead ?
The text was updated successfully, but these errors were encountered: