ap
axios-parallel
npm i axios-parallel
ap

axios-parallel

Multithreaded async requests sender using Axios and Workers

by Jove Jonovski

1.1.2 (see all)License:MITTypeScript:DefinitelyTyped
npm i axios-parallel
Readme

Send Asynchronous Requests

Send multiple asynchronous requests using Axios & Workers in NodeJS

NOTE: This module is meant for Node.JS applications, not Web Browser.

Installation

npm install axios-parallel --save

Usage

Response schema can be found here


JavaScript Example

const axiosParallel = require('axios-parallel');

// Debug
const { performance } = require('perf_hooks');
const { writeFileSync } = require('fs');

console.log('Start...');

(async () => {
  const start = performance.now();
  const requests = [];

  // https://jsonplaceholder.typicode.com/posts
  for (let id = 1; id <= 100; id++) {
    // https://github.com/axios/axios#axios-api
    requests.push({
      method: 'GET',
      url: `https://jsonplaceholder.typicode.com/posts/${id}`
    });
  }

  try {
    const MAX_PARALLEL_REQUEST_PER_CPU = 30;
    const response = await axiosParallel(requests, MAX_PARALLEL_REQUEST_PER_CPU);

    writeFileSync('example.response.json', JSON.stringify(response), {
      encoding: 'utf8'
    });
  } catch (error) {
    throw new Error(error);
  }

  const end = performance.now() - start;
  console.log(`Execution time: ${end}ms`);
})();

TypeScript Example

import axiosParallel, { AxiosParallelInput, AxiosParallelResponse } from 'axios-parallel';

// Debug
import { performance } from 'perf_hooks';
import { writeFileSync } from 'fs';

console.log('Start...');

(async () => {
  const start = performance.now();
  const requests: AxiosParallelInput = [];

  // https://jsonplaceholder.typicode.com/posts
  for (let id = 1; id <= 100; id++) {
    // https://github.com/axios/axios#axios-api
    requests.push({
      method: 'GET',
      url: `https://jsonplaceholder.typicode.com/posts/${id}`
    });
  }

  try {
    const MAX_PARALLEL_REQUEST_PER_CPU = 30;
    const response: AxiosParallelResponse[] = await axiosParallel(requests, MAX_PARALLEL_REQUEST_PER_CPU);

    writeFileSync('example.response.json', JSON.stringify(response), {
      encoding: 'utf8'
    });
  } catch (error) {
    throw new Error(error);
  }

  const end = performance.now() - start;
  console.log(`Execution time: ${end}ms`);
})();

Downloads/wk

38

GitHub Stars

10

LAST COMMIT

10mos ago

MAINTAINERS

1

CONTRIBUTORS

0

OPEN ISSUES

1

OPEN PRs

0
VersionTagPublished
1.1.2
latest
10mos ago
No alternatives found
No tutorials found
Add a tutorial