Skip to content

feat: add abort() method to settle pending tasks#101

Merged
mcollina merged 1 commit into
masterfrom
add-abort-method
Dec 23, 2025
Merged

feat: add abort() method to settle pending tasks#101
mcollina merged 1 commit into
masterfrom
add-abort-method

Conversation

@mcollina

Copy link
Copy Markdown
Owner

Summary

This PR adds a new abort() method that properly handles pending tasks by calling their callbacks with an abort error and rejecting promises. This fixes issue #97 where kill() left promises unsettled, causing "Detected unsettled top-level await" warnings.

Changes

  • New abort() method in queue.js that:

    • Iterates through all queued tasks
    • Calls the error handler (if set) for each task with an abort error
    • Calls each task's callback with Error('abort')
    • For promise-based queues, this results in promise rejection
    • Properly releases tasks back to the object pool
    • Resets the drain function
  • TypeScript definitions updated in index.d.ts to include the new method

  • Comprehensive test coverage added:

    • Callback API tests in test/test.js
    • Promise API tests in test/promise.js
    • Error handler integration test

Differences from kill()

Unlike kill() which simply discards queued tasks:

  • abort() calls each queued task's callback with Error('abort')
  • For promise-based queues, all pending promises are properly rejected
  • The global error handler (if set) is invoked for each aborted task
  • Tasks are properly cleaned up and released back to the object pool

Test Results

  • ✅ All 256 tests pass
  • ✅ 100% code coverage (lines, branches, functions)
  • ✅ Linting passes

Example Usage

import { promise } from 'fastq'
import { setTimeout } from 'node:timers/promises'

const queue = promise(doWork, 1)
const promises = []

for(let i = 0; i < 10; i++) {
  promises.push(queue.push())
}

queue.abort()

// All promises are now rejected with Error('abort')
await Promise.allSettled(promises)
console.log('All promises settled')

async function doWork() {
  await setTimeout(500)
}

Fixes #97

🤖 Generated with Claude Code

Add new abort() method that properly handles pending tasks by calling
their callbacks with an abort error and rejecting promises. This fixes
the issue where kill() left promises unsettled, causing "Detected
unsettled top-level await" warnings.

Unlike kill() which simply discards queued tasks, abort():
- Calls each queued task's callback with Error('abort')
- Rejects promises for promise-based queues
- Calls the global error handler (if set) for each aborted task
- Properly cleans up task objects and resets drain function

Fixes #97

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@mcollina mcollina merged commit f253408 into master Dec 23, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warning: Detected unsettled top-level await on queue#kill()

1 participant