title: Taro.createWorker(scriptPath)

sidebar_label: createWorker

Creates a worker thread. You can create only one worker at most. Call Worker.terminate before creating another worker.

Reference

Type

  1. (scriptPath: string) => Worker

Parameters

Property Type Description
scriptPath string The absolute path to the worker entry file.

Sample Code

  1. const worker = Taro.createWorker('workers/request/index.js')
  2. worker.onMessage(function (res) {
  3. console.log(res)
  4. })
  5. worker.postMessage({
  6. msg: 'hello worker'
  7. })
  8. worker.terminate()

API Support

API WeChat Mini-Program H5 React Native
Taro.createWorker ✔️