86760929dab8b61c2c5bd7fe62f576473efb8dcc
[poolifier.git] / examples / typescript / http-client / node-fetch / src / main.ts
1 import { availableParallelism } from 'poolifier'
2 import { fetchPool } from './pool.js'
3 import { type WorkerResponse } from './types.js'
4
5 const parallelism = availableParallelism()
6 const requestUrl = 'http://localhost:8080/'
7
8 const fetchPoolPromises = new Set<Promise<WorkerResponse>>()
9 for (let i = 0; i < availableParallelism(); i++) {
10 fetchPoolPromises.add(fetchPool.execute({ url: requestUrl }))
11 }
12
13 try {
14 const now = performance.now()
15 const responses = await Promise.all(fetchPoolPromises)
16 const elapsedTime = performance.now() - now
17 console.info(
18 `Received in ${elapsedTime.toFixed(2)}ms an array with ${
19 responses.length
20 } responses from ${parallelism} parallel requests made with node-fetch on ${requestUrl}:\n`,
21 responses
22 )
23 } catch (error) {
24 console.error(error)
25 }