So far, we've see how to use workers to perform a variety of tasks, but we've also seen that they can be cumbersome to use. This is unavoidable to some extent. However, we can provide nice interfaces to operations that are using workers with the tools we've already seen in in previous chapters.
In this recipe, we'll see how to use promises to create more familiar interfaces.
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
- 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 amain
function frommain.js
. - Create a
main.js
file, with anasync
function namedsumOnWorker
:
// main.js async function sumOnWorker(array) {}
- Inside
sumOnWorker
, return a new promise, wherein you create a new...