0.2.0 • Published 7 years ago
fork-acknowledge v0.2.0
fork-acknowledge
Get acknowledgements back from messages sent to forked processes
Install
npm i fork-acknowledgeUsage
parentconst { fork } = require('child_process') const fack = require('fork-acknowledge') const child = fork(...); const { send } = fack(child); const result = await send('message'); └────────────────────────────────────────┐childconst fack = require('fork-acknowledge') │ │ const { on } = fack(process); │ │ on(async message => { │ return 'result' ───────────────────────────────┘ });
API
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 toprocessconst promise = send(...args)promise[promise]- Resolves (or rejects) when the corresponding¹onmethod on theprocessreturns (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 fromprocessconst off = on(fn)off[function]- Function to remove the event listener after whichfnwill 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).