Skip to content

Instantly share code, notes, and snippets.

@liyaodong
Last active June 29, 2022 15:02
Show Gist options
  • Save liyaodong/6a04dfbe5fef25e1773a4ce8b27eea93 to your computer and use it in GitHub Desktop.
Save liyaodong/6a04dfbe5fef25e1773a4ce8b27eea93 to your computer and use it in GitHub Desktop.
chunkPromise
const _ = require("lodash");
const sequential = require("promise-sequential");
/**
* Batch execute promises in chunks
*/
const chunkPromise = (promises, chunkSize = 5) => {
const chunkedPromise = _.chunk(promises, chunkSize).map(
(promiseGroup) => () =>
Promise.all(promiseGroup.map((promiseFn) => promiseFn()))
);
const result = sequential(chunkedPromise).then((chunkedData) =>
_.flatten(chunkedData)
);
return result;
};
module.exports = {
chunkPromise,
};
import _ from 'lodash';
import {chunkPromise} from '../chunkPromise';
describe('ChunkPromise', (): void => {
test('should call all promises', async (): Promise<void> => {
const promiseExample = (): Promise<string> =>
new Promise((resolve): void => {
const result = Math.trunc(Date.now() / 1000);
setTimeout((): void => resolve(String(result).slice(-1)), 1000);
});
const sampleLength = 11;
const promises = _.range(sampleLength).map(
(): (() => Promise<string>) => promiseExample
);
const chunkSize = 5;
const result = await chunkPromise(promises, chunkSize);
expect(result).toHaveLength(sampleLength);
expect(
_.chunk(result, chunkSize).every((vals): boolean =>
vals.every((v): boolean => v === vals[0])
)
);
});
});
import _ from 'lodash';
import sequential from 'promise-sequential';
/**
* Batch execute promises in chunks
*/
export const chunkPromise = <T>(
promises: (() => Promise<T>)[],
chunkSize = 5
): Promise<T[]> => {
const chunkedPromise = _.chunk(
promises,
chunkSize
).map((promiseGroup: (() => Promise<T>)[]): (() => Promise<
Awaited<T>[]
>) => (): Promise<Awaited<T>[]> =>
Promise.all(
promiseGroup.map(
(promiseFn: () => Promise<T>): Promise<T> => promiseFn()
)
)
);
const result = sequential(chunkedPromise).then((chunkedData: T[]): T[] =>
_.flatten<T>(chunkedData)
);
return result as Promise<T[]>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment