Shows the use of generators to control concurrency. This example illustrates the following.
- 3 workers running through the data
- Each piece of "work" takes 200-2000 milliseconds to run
- The 5th, 10th and 17th item will throw an exception
- Workers will exit because the exception is unhandled
worker='1' itemnum='0' item='F'
worker='2' itemnum='1' item='a'
worker='1' itemnum='3' item='s'
worker='3' itemnum='2' item='l'
worker='2' itemnum='4' item='c'
worker='3' itemnum='6' item='e'
worker='3' itemnum='8' item=' '
worker='2' itemnum='7' item='s'
worker='3' itemnum='9' item='Ü'
worker='3' itemnum='11' item='e'
worker='3' itemnum='12' item='n'
worker='3' itemnum='13' item=' '
worker='3' itemnum='14' item='v'
worker='3' itemnum='15' item='o'
worker='3' itemnum='16' item='n'
Simpler example than above, with added exception handling.
- 3 workers running through the data
- Each piece of "work" takes 200 milliseconds to run
- The 5th, 10th and 17th item will throw an exception
- The script will finish all 57 items
itemnum='0' item='F'
...
itemnum='16' item='n'
Error: Error at 17:
at /home/phadviger/code/demo/gist.generator-concurrency/simple-example.js:12:17
itemnum='19' item='y'
...
itemnum='57' item='g'
Contains a reusable method called mapC
which actually performs a map, instead
of just using the map method to loop.
Using
Promise.all
instead ofPromise.allSettled
yields much easier fail fast exception handling, but it just depends on how you want to handle failures.