Skip to content

Commit fa27c2f

Browse files
committed
Add createWorker to the docs
1 parent 11d0df4 commit fa27c2f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/usage.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ const worker = await spawn(BlobWorker.fromText(MyWorker))
166166

167167
Bundle this module and you will obtain a stand-alone bundle that has its worker inlined. This is particularly useful for libraries using threads.js.
168168

169+
### createWorker - select worker backend
170+
`createWorker` allows selecting the worker backend (among web, node, and tiny), and also if you want a blob worker. The second argument to the `createWorker` is an object that specifies `backend: 'web' | 'node' | 'tiny'` and `blob: boolean`.
171+
172+
`createWorker` uses dynamic imports to only import the needed implementation, so you can import the needed functions directly to reduce the bundle size.
173+
174+
```js
175+
import { createWorker, spawn, Thread } from "threads"
176+
177+
async function run() {
178+
const worker = await createWorker("./worker.js", {backend: "node"})
179+
const add = await spawn(worker)
180+
const result = await add(2, 3)
181+
await Thread.terminate(add)
182+
return result
183+
}
184+
185+
run().then(result => {
186+
console.log(`Result: ${result}`)
187+
}).catch(error => {
188+
console.error(error)
189+
})
190+
```
191+
192+
169193
## TypeScript
170194

171195
### Type-safe workers

0 commit comments

Comments
 (0)