fix: execute() can have no arguments in examples
[poolifier.git] / examples / fixedExample.js
CommitLineData
6961ca9a
JB
1const {
2 FixedThreadPool,
3 PoolEvents,
4 availableParallelism
5} = require('poolifier')
6
7const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
cf9aa6c3 8 errorHandler: e => console.error(e),
53795b86 9 onlineHandler: () => console.info('worker is online')
cf9aa6c3 10})
2431bdb4 11let poolReady = 0
6961ca9a 12let poolBusy = 0
2431bdb4 13pool.emitter.on(PoolEvents.ready, () => poolReady++)
aee46736 14pool.emitter.on(PoolEvents.busy, () => poolBusy++)
3e460d6d 15
6961ca9a 16let resolved = 0
15e5141f 17const start = performance.now()
bf962cba 18const iterations = 1000
292ad316 19for (let i = 1; i <= iterations; i++) {
583a27ce 20 pool
8923de44 21 .execute()
7a6a0a96 22 .then(() => {
583a27ce
JB
23 resolved++
24 if (resolved === iterations) {
53795b86 25 console.info('Time taken is ' + (performance.now() - start))
2431bdb4 26 console.info('The pool was ready for ' + poolReady + ' times')
53795b86 27 return console.info('The pool was busy for ' + poolBusy + ' times')
583a27ce
JB
28 }
29 return null
30 })
31 .catch(err => console.error(err))
6dc67cda 32}