Sobes.tech
Junior — Senior

Parallel query execution with limit on the number

livecode

Task condition

Create a function that, for each element of the input array, calls the provided callback and collects the results into an array of responses Response[].

The requests should be executed concurrently, but the number of simultaneous requests is limited by the parameter limit. This scheme helps reduce the total execution time of the program without overloading resources.

 type Props<RequestData, Response> = {
 callback: (args: RequestData) => Promise<Response>;
 data: Array<RequestData>;
 limit: number;
};

/*                    Solution to the problem                */
export async function runWithLimit<RequestData, Response>({
 callback,
 data,
 limit,
}: Props<RequestData, Response>): Promise<Response[]> {
 /*    START SOLUTION HERE  */
 const result: Response[] = [];

 return Promise.all([]);
}