prefetch-iterator
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Installation

npm i prefetch-iterator

Usage

import { PrefetchIterator } from 'prefetch-data-iterator'
import { MongoClient } from 'mongodb'

type State = {
    skip: number
    limit: number
}
type Payload = any[]

const mongo = new MongoClient('mognodb://localhost')
await mongo.connect()

const iterator = new PrefetchIterator<State, Payload>({
    prefetch: 3, // Will try to keep 3 batches of transactions prefetched
    initialState: {
        skip: 0,
        limit: 10000
    },
    fetchNextPayload (state) {
        const payload = await mognodb.db().collection('transactions')
            .find()
            .skip(state.skip)
            .limit(state.limit)
            .toArray()
        return {
            payload,
            state: {
                ...state,
                skip: state.skip + payload.length
            }
        }
    }
})

for async (const payload of iterator.items()) {
    await someHeavyProcessing(payload)
}

Readme

Keywords

none

Package Sidebar

Install

npm i prefetch-iterator

Weekly Downloads

4

Version

0.1.2

License

none

Unpacked Size

6.24 kB

Total Files

8

Last publish

Collaborators

  • boo1ean