fork-with-emitter
Simple EventEmitter wrapper for IPC, enhanced with async .request().
- Zero dependencies.
- TypeScript support.
- Intuitive naming (fork/host).
Basics
bot.js
(fork):
const host = host host //returns promise that resolves after given msconst sleep =
index.js
(host):
const createFork = const fork = //pipe fork's console.log to host's console.logforkprocessstdout fork ;async { const randomNumber = await fork console fork}
Output:
Hello Artur623
Handling errors
bot.js
(fork):
const host = host
index.js
(host):
const createFork = const fork = ;async { try await fork catcherror console }
Output:
Error: Some error message at
Errors and rejections are captured only from .onRequest() handlers.
Exports
/* Returns new spawned fork. */ /* Variable indicating if process is a fork. */ isForked /* Points to host (use those methods from fork). */ host: /* process.on('message', listener) with events */ /* Listener is removed after execution. */ /* process.send(payload) with events */ /* Returned/resolved data from async function will be passed to host's request. */ /* Returns Promise that resolves with data resolved from host's .onRequest() listener. Rejects if response is not sent after 10 seconds. maximumTimeout = Infinity -> for very long tasks, not recommended though, because if task stucks and fork still works it causes a memory leak. */
Fork object
Object that points to spawned fork.
/* Native ChildProcess object. */ process /* Exits process with SIGINT. */
License
MIT