Execute node modules as child processes (child_process.fork wrapper).
// parent.js
var Task = require('async-task')
var task = await Task.createTaskManager('./task.js')
await task.set('customData', 'a simple string')
await task.set('document', {
id: 123,
name: 'example.pdf'
})
var result = await task.execute()
// task.js
var Task = require('async-task')
var T = Task.create()
T.main(function onExecuteCall() {
var customData = T.get('customData') // 'a simple string'
var doc = T.get('document') // { id: 123, name: 'example.pdf' }
var result = cpuIntensiveTask()
T.resolve(result)
})
Creates a task manager.
var task = await Task.createTaskManager('./example-task.js')
Tells forked process to execute main function and returns a promise with the result.
var task = await Task.createTaskManager('./example-task.js')
var result = await task.execute()
Sets custom data inside the forked process.
var task = await Task.createTaskManager('./example-task.js')
task.set('input', 42)
Sets the current script as a Task.
var T = Task.create()
Sets the function to be executed.
var T = Task.create()
T.main(async function doThis() {
/* ... */
})
Get custom data setted from parent process.
var T = Task.create()
T.main(async function doThis() {
var data = T.get('customData')
/* ... */
})
Communicates the result to the parent process.
var T = Task.create()
T.main(async function getUsers() {
/* ... */
T.resolve(users)
})