8000 GitHub - fermads/spawn-function: Spawn a child_process and run a function in it. Get the function's results on your main process using a stream or callback
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Spawn a child_process and run a function in it. Get the function's results on your main process using a stream or callback

Notifications You must be signed in to change notification settings

fermads/spawn-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

spawnFunction

Spawn a child_process and run a function in it. Get the function's results on your main process using a stream or callback

Install

npm install spawn-function

Usage

Callback mode

spawnFunction(isolatedFunction[, options][, callback])
  • isolatedFunction: Function to be executed on the spawned process. Must be a self-contained/isolated function
  • options: Object to be passed to child_process.spawn()
  • callback(error, data): Callback to be called when isoldatedFunction is done

Example

let spawnFunction = require('spawn-function')

spawnFunction(function () {
  let fib = function (n) {
    if (n < 2)
      return 1
    else
      return fib(n - 2) + fib(n - 1)
  }
  return fib(41)
}, function (error, data) {
  if (error) return console.log(error)
  console.log('Fibonacci result is', data)
})

Stream mode

let stream = spawnFunction(isolatedFunction[, options])
  • stream: Returned stream
  • isolatedFunction: Function to be executed on the spawned process. Must be a self-contained/isolated function.
  • options: Object to be passed to child_process.spawn()

Example

let stream = spawnFunction(function () {
  return 1
})

let chunk = []
stream.on('data', function (data) {
  chunk.push(data)
})

stream.on('end', function () {
  console.log(chunk.join(''))
})

stream.on('error', function (err) {
  console.log(err)
})

About

Spawn a child_process and run a function in it. Get the function's results on your main process using a stream or callback

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0