sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.48k stars 185 forks source link

How to properly handle exceptions to avoid process exiting? #172

Closed galaczi closed 1 year ago

galaczi commented 1 year ago

According to the docs:

Note: If your items can potentially throw an exception, you must handle those errors from the returned Promise or they may be reported as an unhandled Promise rejection and potentially cause your process to exit immediately.

I tried this:

      try {
        await queue.add(async () => {
          handleClosed()
        })
      } catch (error) {
        console.log('Error thrown in queue.')
      }

Still, if an error is thrown in handleClosed, the whole process exits. What am I missing?

Edit: same result with this:

      await queue.add(async () => {
        try {
          handleClosed()
        } catch (error) {
          console.log('Error thrown in queue.')
        }
      })

Edit 2: Maybe I am misunderstanding what the docs say. I shouldn't throw errors inside jobs at all (or should catch inside)?

galaczi commented 1 year ago

Anyone knows how to avoid the main process exiting on error in any of the jobs? I found some old issues, people asking for it. Why would you want the whole process to exit on error in a single job, I don't get it.

AndersCan commented 1 year ago

I think you are missing an await

await queue.add(async () => {
  try {
    await handleClosed()
  } catch (error) {
    console.log('Error thrown in queue.')
  }
})