I'm using NodeJs execFile() to start a daemon process. I've wrapped the callback in a promise to use it asynchronously in other parts of my application.
I understand callbacks emit err/result upon completion. So when the daemon process fails, the promise rejects the error correctly.
Problem: when the daemon process starts correctly, the promise doesn't resolve anything, since the callback has not completed yet.
The response is received once the daemon process is stopped since that is the end of the callback lifecycle. But that's too late.
Question: How do I track the life cycle of the callback to let the promise resolve(callback_in_progress)?
Do I need to use a node event module to create event listener?
These are 2 methods from the module. The first works well, but the second is where I'm having a problem.
const { execFile } = require('child_process');
const path = require('path');
const binaryPath = require('./BinaryPaths')
const mcUtil = path.join(binaryPath, 'multichain-util');
const mcd = path.join(binaryPath, 'multichaind');
module.exports = {
createChain: (chainName) => {
return new Promise((resolve, reject) => {
execFile(mcUtil, ['create', chainName], (err, res) => {
err ? reject(err) : resolve(res);
});
});
},
startMultichain: (chainName) => {
return new Promise((resolve, reject) => {
execFile(mcd, [chainName, 'daemon'], (err, res) => {
err ? reject(err.message) : resolve(res);
});
});
},
};