Skip to content

Latest commit

 

History

History
105 lines (78 loc) · 1.89 KB

README.md

File metadata and controls

105 lines (78 loc) · 1.89 KB

async-task

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)
})

TaskManager API

Creation

Creates a task manager.

Task.createTaskManager(modulePath, [args, [options]]): TaskManager

var task = await Task.createTaskManager('./example-task.js')

Tell child to execute main function

Tells forked process to execute main function and returns a promise with the result.

taskManager.execute()

var task = await Task.createTaskManager('./example-task.js')
var result = await task.execute()

Set custom data on the fly

Sets custom data inside the forked process.

taskManager.set(key: string, value: any)

var task = await Task.createTaskManager('./example-task.js')
task.set('input', 42)

Task API

Sets the current script as a Task.

Task.create()

var T = Task.create()

Sets the function to be executed.

T.main(fn: Function)

var T = Task.create()
T.main(async function doThis() {
    /* ... */
})

Get custom data setted from parent process.

T.get()

var T = Task.create()
T.main(async function doThis() {
    var data = T.get('customData')
    /* ... */
})

Communicates the result to the parent process.

T.resolve()

var T = Task.create()
T.main(async function getUsers() {
    /* ... */
    T.resolve(users)
})

TODO