Handle timeouts and cancellation
masterYou can manage process lifecycles in two ways:
- Via options: Pass
timeoutandkillSignalas the third argument toyoutubedl.exec()(which maps tospawn#options). - Programmatically: Use the subprocess object returned by
youtubedl.exec()to call.kill()or.cancel().
Note: The main youtubedl() function returns a Promise, while youtubedl.exec() returns the subprocess object.
// Method 1: Using spawn options
const url = 'https://www.youtube.com/watch?v=6xKWiCMKKJg'
const result = await youtubedl.exec(url, ,{ dumpSingleJson: true }, {
timeout: 5000,
killSignal: 'SIGKILL'
})
// Method 2: Manual cancellation
const subprocess = youtubedl.exec(url, { dumpSingleJson: true })
setTimeout(() => {
subprocess.kill('SIGKILL')
}, 5000)
const result = await subprocess