Experiment 02 · Async Generators · Multiple subworkers

Three async range( min, max, step, delay) generator functions run by distinct subworkers, each one in its own thread. The subworkers aggregate the values of the async generator and return them all at once in an array. Here again, the delay determines the time in milliseconds in between delivery of every single value of a range.

const worker = new Worker( "worker.js");
worker.postMessage( { name: "range+0070", op: "range", args: [ 1, 70, 30 ] });             // no delay
worker.postMessage( { name: "range+0200", op: "range", args: [ 100, 200, 10, 400 ] });     // 400ms delay
worker.postMessage( { name: "range-0200", op: "range", args: [ -100, -200, -10, 400 ] });  // 400ms delay
worker.postMessage( { name: "range+0500", op: "range", args: [ 100, 500, 50, 200 ] });     // 200ms delay
worker.postMessage( { name: "range-0500", op: "range", args: [ -100, -500, -50, 200 ] });  // 200ms delay
worker.postMessage( { name: "range+6000", op: "range", args: [ 1000, 6000, 500, 5 ] });    // 5ms delay
worker.postMessage( { name: "range-6000", op: "range", args: [ -1000, -6000, -500, 5 ] }); // 5ms delay
worker.postMessage( { name: "namedOp01", op: "sip", args: [ "☕️" ] });
worker.postMessage( { name: "namedOp02", op: "crunch", args: [ "🍐", "🍪" ] });
worker.postMessage( { name: "namedOp03", op: "listen", args: [ "Djesse / Every Little Thing She Does Is Magic" ] });

Supported by Firefox 64 and Chrome 71 out-of-the box — but not by Safari 12, which throws  Reference Error: Can't find variable: Worker .