Using node child process exec
, I'm calling a ffmpeg conversion via a promise that takes a bit of time. Each time the use clicks "next" it starts the FFMpeg command on a new file:
function doFFMpeg(path){
return new Promise((resolve, reject) => {
exec('ffmpeg (long running command)', (error, stdout, stderr) => {
if (error) {
reject();
}
}).on('exit', (code) => { // Exit returns code 0 for good 1 bad
if (code) {
reject();
} else {
resolve();
}
});
});
}
The problem is, if the user moves on to the next video before the promise is returned, I need to scrap the process and move on to converting the next video.
How do I either:
A) (Ideally) Cancel the current promised exec process* B) Let the current promised exec process complete, but just ignore that promise while I start a new one.
*I realize that promise.cancel is not yet in ECMA, but I'd like to know of a workaround -- preferably without using a 3rd party module / library.
Attempt:
let myChildProcess;
function doFFMpeg(path){
myChildProcess.kill();
return new Promise((resolve, reject) => {
myChildProcess = exec('ffmpeg (long running command)', (error, stdout, stderr) => {
if (error) {
reject();
}
}).on('exit', (code) => { // Exit returns code 0 for good 1 bad
if (code) {
reject();
} else {
resolve();
}
});
});
}