microlinkhq / youtube-dl-exec

A simple Node.js wrapper for youtube-dl/yt-dlp.
MIT License
423 stars 75 forks source link

Incorrect return type for exec function #196

Closed mikeyaworski closed 4 months ago

mikeyaworski commented 4 months ago

Moving this issue https://github.com/microlinkhq/tinyspawn/issues/13 to this library, since I misunderstood where the error was being thrown and how to handle it. I'll write out my confusion for future readers.

I am doing something like this:

const process = require('youtube-dl-exec').exec(url, {
  output: '-',
  quiet: true,
  format: '...',
  ...
}, {
  stdio: ['ignore', 'pipe', 'ignore'],
});

At this point, process is a Promise that can reject. Because I use TypeScript and the return type for exec is ChildProcess, I didn't realize that it was a Promise, so I didn't think it was possible for me to handle the rejection. I thought an error was being thrown in some event handler that I couldn't prevent without removing all event listeners.

But I'm able to tell TypeScript to be silent and actually handle the rejection with:

// @ts-ignore
process.catch(err => {
  ...
});

And no more unhandled rejection.

TL;DR: I believe the return type for the exec function is inaccurate because it's actually an extension of a Promise.

https://github.com/microlinkhq/youtube-dl-exec/blob/33dcbc3d5644b500ab45ccce4d7b703b55b91298/src/index.d.ts#L394-L396

Kikobeats commented 4 months ago

Fixed in the last version; can you try it and tell me if it works as expected now? 🙏

mikeyaworski commented 4 months ago

Works as expected now. Thanks!