UNPKG

contentful-management

Version:
38 lines (35 loc) 1.31 kB
import copy from 'fast-copy'; const asIterator = (fn, params) => { return { [Symbol.asyncIterator]() { let options = copy(params); const get = () => fn(copy(options)); let currentResult = get(); return { current: 0, async next() { const { total = 0, items = [], skip = 0, limit = 100 } = await currentResult; if (total === this.current) { return { done: true, value: null }; } const value = items[this.current++ - skip]; const endOfPage = this.current % limit === 0; const endOfList = this.current === total; if (endOfPage && !endOfList) { options = { ...options, query: { ...options.query, skip: skip + limit, }, }; currentResult = get(); } return { done: false, value }; }, }; }, }; }; export { asIterator }; //# sourceMappingURL=as-iterator.js.map