简体   繁体   中英

Is it possible to load web worker inside a promise?

Hi I'm really new to Angular and trying to understand web workers.

So far I have two async workers but here is now my problem. The first one is taking too long until it renders the output. The second one has already finished and rendered everything in the frontend. The user can interact with the site even if the first worker isn't ready yet. But this will cause sometimes problems.

So my solution is to encapsulate the tow workers with a promise and call eg $q.all() to render the output of both workers when they have finished gathering all needed informations.

But my current understanding for the initialization of web workers is that I can trigger them eg in a
- webpack.config.js
or
- tsconfig.worker.json So I don't know how or if I can't call all workers by my own script.

It should be possible, but not in the traditional way. After 1 minute of Googling I found an article on Codeburst, explaining the what's and how's of async workers:

https://codeburst.io/promises-for-the-web-worker-9311b7831733

In a nutshell: by wrapping a worker in a Class and instantiating that Class , binding the onmessage attribute on construct , you can fully intercept/handle all functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM