docs: switch to `mapExecute()` in examples
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 30 Aug 2024 16:03:10 +0000 (18:03 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 30 Aug 2024 16:03:10 +0000 (18:03 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
examples/typescript/http-client-pool/src/main.ts
examples/typescript/smtp-client-pool/src/main.ts

index 1103c4ef9378abf550bac5fb22310b3e079a8055..9848d21f1df25fc505ff53cea3a0c16e81883afb 100644 (file)
@@ -1,6 +1,6 @@
 import { availableParallelism } from 'poolifier'
 
-import type { WorkerResponse } from './types.js'
+import type { WorkerData } from './types.js'
 
 import { httpClientPool } from './pool.js'
 
@@ -8,15 +8,16 @@ const parallelism = availableParallelism() * 2
 const requestUrl = 'http://localhost:8080/'
 
 for (const workerFunction of ['node_fetch', 'fetch', 'axios']) {
-  const httpClientPoolPromises = new Set<Promise<WorkerResponse>>()
+  const httpClientRequests = new Set<WorkerData>()
   for (let i = 0; i < parallelism; i++) {
-    httpClientPoolPromises.add(
-      httpClientPool.execute({ input: requestUrl }, workerFunction)
-    )
+    httpClientRequests.add({ input: requestUrl })
   }
   try {
     const now = performance.now()
-    const responses = await Promise.all(httpClientPoolPromises)
+    const responses = await httpClientPool.mapExecute(
+      httpClientRequests,
+      workerFunction
+    )
     const elapsedTime = performance.now() - now
     console.info(
       `Received in ${elapsedTime.toFixed(
index f9f4d78e9abf40bcc284cdb30331f8ad6e1c84a3..a06a800c60dd88652b4b41140d75371b1e9d5046 100644 (file)
@@ -1,35 +1,34 @@
-import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js'
+import type { WorkerData } from './types.js'
 
 import { smtpClientPool } from './pool.js'
 
 const tos = ['bar@example.com, baz@example.com']
 
-const smtpClientPoolPromises = new Set<Promise<SMTPTransport.SentMessageInfo>>()
+const smtpMessages = new Set<WorkerData>()
 for (const to of tos) {
-  smtpClientPoolPromises.add(
-    smtpClientPool.execute({
-      mail: {
-        from: '"Foo" <foo@domain.tld>',
-        html: '<b>Hello world?</b>',
-        subject: 'Hello',
-        text: 'Hello world?',
-        to,
+  smtpMessages.add({
+    mail: {
+      from: '"Foo" <foo@domain.tld>',
+      html: '<b>Hello world?</b>',
+      subject: 'Hello',
+      text: 'Hello world?',
+      to,
+    },
+    smtpTransport: {
+      auth: {
+        pass: 'REPLACE-WITH-YOUR-GENERATED-PASSWORD',
+        user: 'REPLACE-WITH-YOUR-ALIAS@DOMAIN.TLD',
       },
-      smtpTransport: {
-        auth: {
-          pass: 'REPLACE-WITH-YOUR-GENERATED-PASSWORD',
-          user: 'REPLACE-WITH-YOUR-ALIAS@DOMAIN.TLD',
-        },
-        host: 'smtp.domain.tld',
-        port: 465,
-        secure: true,
-      },
-    })
-  )
+      host: 'smtp.domain.tld',
+      port: 465,
+      secure: true,
+    },
+  })
 }
+
 try {
   const now = performance.now()
-  await Promise.all(smtpClientPoolPromises)
+  await smtpClientPool.mapExecute(smtpMessages)
   const elapsedTime = performance.now() - now
   console.info(
     `Send in parallel in ${elapsedTime.toFixed(