I am a bit new to child-process and require an additional clarification about it.
My case
I'm looking for way to run/trigger one js file as a separate/isolated process from another js file. Without using direct require or import as, but pass a certain argument (number in my case) from parent to child process.
parent.js
async function parent () {
/** logic */
/** run/start child.js script with certain (1) argument from logic above /
/** if logic below fails, it won't crash/trigger child, and his job will be finished separately outside async function*/
}
child.js
async function child(number) {
/** receive argument from parent.js and execute separately*/
}
Just to be sure, schema:
MAIN THREAD
|
|
run child => CHILD THREAD (receive number arg from main thread)
| |
x | (don't care about error in main thread)
(error) |
END OF M_THREAD |
|
V
(ok, filled)
So I have read could of articles about child_process and official node.js docs, and know that there are:
- fork
- exec
- child
modes. And I could also run a separate npm task from my parent.js file (via pm2 for example), but I have no idea what should I use and what suits my needs in this case. So any advice, explanation will be helpful and will be rewarded by up-voting from me.