module
類型的 Worker 。因此,在初始化新的 Worker 實體時需要加入 type option :// Goodnew Worker(new URL("worker.js", import.meta.url).href, { type: "module" });// Badnew Worker(new URL("worker.js", import.meta.url).href);new Worker(new URL("worker.js", import.meta.url).href, { type: "classic" });new Worker("./worker.js", { type: "module" });
main.ts
以來用來派發的工作程式碼 worker.ts
:main.ts
時會有讀取 worker.ts
的動作,所以要記得加入 --allow-read
標籤:worker.ts
移動到遠端上:--allow-net
:--allow-read
標籤唷!眼尖的讀者應該有發現該範例在console.log("hello world");
前面多了一個await
。
Deno
在 Worker 範圍內是不可用的。deno: true
選項做到這點:--unstable
旗標:補充:當Deno
命名空間在 Worker 作用域中可用時,將會繼承main.js
的權限(使用--allow-*
標誌指定的權限)。
There are no limits, you can create as many as you want Again, there are no limits. We now have facilities to set heap limits on the isolate, but it's not possible to set them for workers.
同樣的事情在不同人眼中可能會有不同的見解、看法。在讀完本篇以後,筆者也強烈建議大家去看看以下文章,或許會對型別、變數宣告...等觀念有更深層的看法唷!