The collect utilities provide powerful iterator-based methods for efficiently fetching large datasets from VK API with automatic pagination, parallel requests, and error handling.
createCollectIterator
Creates an async iterator that automatically handles pagination for VK API methods returning large datasets.
import { createCollectIterator } from 'vk-io' ;
const iterator = createCollectIterator < ItemType >({
api ,
method: 'wall.get' ,
params: { owner_id: - 1 },
countPerRequest: 100 ,
maxCount: 1000 ,
parallelRequests: 25
});
for await ( const data of iterator ) {
console . log ( `Progress: ${ data . percent } %` );
console . log ( `Items: ${ data . items . length } ` );
}
Options
VK API instance to use for making requests.
VK API method name (e.g., 'wall.get', 'groups.getMembers').
Parameters to pass to the API method. Can include: Maximum number of items to fetch in total.
Starting offset for pagination.
Any method-specific parameters.
Number of items to fetch per API request (e.g., 100).
Maximum total number of items to collect.
Number of retry attempts on error.
Number of parallel requests to execute (1-25). Uses execute method for parallel requests.
Yielded Data
Each iteration yields an object with the following structure:
Total number of items received so far.
Progress percentage (0-100).
Total number of items available.
Array of items received in this batch.
User profiles returned with the items (if applicable).
Group objects returned with the items (if applicable).
Chain
Chain class for batching multiple API requests using VK’s execute method.
import { vk } from './instance' ;
const chain = vk . api . chain ();
// Add requests to the chain
const promise1 = chain . append ( 'users.get' , { user_ids: 1 });
const promise2 = chain . append ( 'groups.getById' , { group_id: 1 });
const promise3 = chain . append ( 'wall.get' , { owner_id: 1 , count: 10 });
// Execute all requests
await chain . run ();
// Get individual results
const users = await promise1 ;
const groups = await promise2 ;
const wall = await promise3 ;
Methods
append
Adds a method to the execution queue.
Parameters for the API method.
Returns: Promise<T> - Promise that resolves with the method result.
Cannot append new requests after calling run(). Will throw ALREADY_STARTED error.
run
Executes all queued requests using execute method.
Returns: Promise<IExecutesPayload>
Array of all successful responses.
Array of errors that occurred during execution.
then
Allows using chain as a promise (calls run() automatically).
const { response , errors } = await chain ;
executes
Executes multiple identical method calls with different parameters.
import { executes } from 'vk-io' ;
const result = await executes ({
api: vk . api ,
method: 'users.get' ,
queue: [
{ user_ids: 1 },
{ user_ids: 2 },
{ user_ids: 3 }
]
});
console . log ( result . response ); // Array of results
console . log ( result . errors ); // Array of errors
Options
VK API method to execute.
Array of parameter objects for each method call.
Returns: Promise<IExecutesPayload>
Usage Examples
Collecting Wall Posts
const posts = [];
const iterator = createCollectIterator ({
api: vk . api ,
method: 'wall.get' ,
params: { owner_id: - 1 , filter: 'owner' },
countPerRequest: 100 ,
maxCount: 500
});
for await ( const data of iterator ) {
posts . push ( ... data . items );
console . log ( `Collected ${ data . received } / ${ data . total } posts` );
}
Collecting Group Members
const members = [];
const profiles = [];
const iterator = createCollectIterator ({
api: vk . api ,
method: 'groups.getMembers' ,
params: {
group_id: '1' ,
fields: [ 'photo_100' , 'city' , 'verified' ]
},
countPerRequest: 1000 ,
parallelRequests: 25
});
for await ( const data of iterator ) {
members . push ( ... data . items );
profiles . push ( ... data . profiles );
console . log ( `Progress: ${ data . percent } %` );
}
Batch Requests with Chain
const chain = vk . api . chain ();
// Queue multiple requests
const userPromise = chain . append ( 'users.get' , {
user_ids: [ 1 , 2 , 3 ],
fields: [ 'photo_100' , 'city' ]
});
const groupPromise = chain . append ( 'groups.getById' , {
group_ids: [ '1' , '2' ],
fields: [ 'members_count' , 'description' ]
});
const wallPromise = chain . append ( 'wall.get' , {
owner_id: 1 ,
count: 20
});
// Execute all at once
const { response , errors } = await chain . run ();
// Or use individual promises
const users = await userPromise ;
const groups = await groupPromise ;
const wall = await wallPromise ;
Parallel Method Execution
// Execute same method with different params
const result = await executes ({
api: vk . api ,
method: 'groups.getById' ,
queue: [
{ group_id: '1' , fields: [ 'members_count' ] },
{ group_id: '2' , fields: [ 'members_count' ] },
{ group_id: '3' , fields: [ 'members_count' ] },
{ group_id: '4' , fields: [ 'members_count' ] }
]
});
result . response . forEach (( group , index ) => {
console . log ( `Group ${ index + 1 } :` , group );
});
if ( result . errors . length > 0 ) {
console . error ( 'Some requests failed:' , result . errors );
}
Error Handling
CollectError
Thrown when collect iterator encounters errors during execution.
try {
for await ( const data of iterator ) {
// Process data
}
} catch ( error ) {
if ( error . code === 'EXECUTE_ERROR' ) {
console . error ( 'Execute errors:' , error . errors );
}
}
Automatic Fallback
The iterator automatically falls back to single requests if:
Token doesn’t support execute method (app auth)
Runtime errors occur with parallel requests
// Automatically handles execute failures
const iterator = createCollectIterator ({
api: vk . api ,
method: 'wall.get' ,
params: { owner_id: 1 },
countPerRequest: 100 ,
parallelRequests: 25 // Falls back to 1 if needed
});
Use parallelRequests: 25 for maximum performance when fetching large datasets. The iterator uses VK’s execute method to batch requests.
The countPerRequest should match the maximum count allowed by the specific API method. For example, wall.get allows up to 100 items per request.
Some tokens (like app access tokens) don’t support the execute method. The iterator automatically detects this and falls back to single requests.