Get acknowledgements back from messages sent to forked processes
npm i fork-acknowledge
-
parent
const { fork } = require('child_process') const fack = require('fork-acknowledge') const child = fork(...); const { send } = fack(child); const result = await send('message'); └────────────────────────────────────────┐
-
child
const fack = require('fork-acknowledge') │ │ const { on } = fack(process); │ │ on(async message => { │ return 'result' ───────────────────────────────┘ });
const {send, on} = fack(process)
-
process
[process]
- process, or a (forked) child_process used to send, or on which to listen for, messages. -
send
[function]
- Function to send messages toprocess
const promise = send(...args)
promise
[promise]
- Resolves (or rejects) when the corresponding¹on
method on theprocess
returns (or throws).promise.off
[function]
- Special function attached to the promise, to remove theprocess.on()
event listener created bysend
, which otherwise is automatically removed upon thepromise
's resolution/rejection.args
[array]
- Message arguments to be sent.
-
on
[function]
- Function to receive messages fromprocess
const off = on(fn)
off
[function]
- Function to remove the event listener after whichfn
will no longer be called.fn
[function]
- Function that gets called when a message is received. It's the return value (or error in case it throws) of this function that's used to resolve or reject the corresponding¹promise
. Correspondence is maintained by sending/receiving a unique id (generated using crypto).