简体   繁体   English

是否可以在诺言中加载网络工作者?

[英]Is it possible to load web worker inside a promise?

Hi I'm really new to Angular and trying to understand web workers. 嗨,我真的是Angular的新手,正在尝试了解网络工作者。

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. 因此,我的解决方案是用诺言封装拖车工人,并调用例如$ q.all(),以在两个工人完成所有所需信息收集后呈现两个工人的输出。

But my current understanding for the initialization of web workers is that I can trigger them eg in a 但是我目前对网络工作者初始化的理解是,我可以在例如
- webpack.config.js -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. -tsconfig.worker.json所以我不知道如何或者是否无法通过自己的脚本调用所有工作者。

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: 在Google搜索1分钟后,我发现了一篇关于Codeburst的文章,解释了异步工作人员的内容和方式:

https://codeburst.io/promises-for-the-web-worker-9311b7831733 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. 简而言之:通过将一个worker包装在一个Class并实例化该Class ,将其onmessage属性绑定到construct ,您可以完全拦截/处理所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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