How to do it...

  1. Open your command-line application, and navigate to your workspace.
  2. Create a new folder named 05-09-using-promise-for-simple-interfaces.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  4. Create a main.js file, with an async function named sumOnWorker:
// main.jsasync function sumOnWorker(array) {}
  1. Inside sumOnWorker, return a new promise, wherein you create a new worker and bind the onmessage event listener, and post a message to the worker to calculate the sum:
// main.jsfunction sumOnWorker(array) {   return new Promise(function (resolve) {     const worker = new Worker('./worker.js');     worker.onmessage = (message) => {};    worker.postMessage({ type: 'calculate-sum', array });   }); } 
  1. Inside the ...

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.