- Open your command-line application, and navigate to your workspace.
- Create a new folder named 05-09-using-promise-for-simple-interfaces.
- Copy or create an index.html that loads and runs a main function from main.js.
- Create a main.js file, with an async function named sumOnWorker:
// main.jsasync function sumOnWorker(array) {}
- 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 }); }); }
- Inside the ...