tencent cloud

All product documents
Tencent Cloud Super App as a Service
Last updated: 2024-11-21 18:41:53
Worker
Last updated: 2024-11-21 18:41:53

wx.CreateWorker

wx.createWorker(string scriptPath)

Function: Create a worker thread.
Parameters: string scriptPath, the absolute path of the worker entry file.
Return:Worker,Worker object.
Notes:
Before using the interface, you need to configure the workers field in app.json, it means the root directory of the worker code;
scriptPath is the absolute path of the entry file, and does not start with /;
Currently, only one worker can be created at most, please calll Worker.terminate before creating the next worker.
Sample code
// Create a regular worker
const worker = wx.createWorker('workers/index.js') // The filename specifies the path to the worker's entry file.


worker.onMessage(function (res) {
console.log(res)
})
worker.postMessage({
msg: 'hello worker'
})
worker.terminate()

Worker

Worker.onMessage

Worker.onMessage(function listener)

Feature Description:Listen to the event of the message sent by the main thread/worker thread to the current thread.
Parameter 1:function listener, the event listener for the message sent by the main thread/worker thread to the current thread.
Parameter 2:Object res
Attributes
Types
Description
message
Object
Message sent from the main/Worker thread to the current thread

Worker.postMessage

Worker.postMessage(Object message)

Feature Description:Message sent to the main/Worker thread.
Parameters:Object message, the message to be sent.
Sample code:
worker thread
worker.postMessage({
msg: 'hello from worker'
})
main thread
const worker = wx.createWorker('workers/request/index.js')
worker.postMessage({
msg: 'hello from main'
})

Woker.terminate

Worker.terminate()

Feature Description:Ends the current Worker thread. Call only on the worker object of the main thread.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon