Skip to content

Alternative cancellation handling with callbacks #552

@aqeelat

Description

@aqeelat

Context

This came up while evaluating @clack/prompts as a potential replacement for enquirer.js, comparing it with enquirer.js and @inquirer/prompts.

Related: #83
Related PR: #544
Split out from: #550

Problem

Every prompt call site that handles Ctrl+C currently needs the same boilerplate:

const result = await confirm({ message: 'Continue?' })

if (isCancel(result)) {
  cancel('Operation cancelled.')
  process.exit(0)
}

This works, but applications with many prompts often centralize cancellation handling. Today, that has to happen after each prompt resolves.

Proposal

Consider supporting an optional cancellation callback on prompts:

const result = await confirm({
  message: 'Continue?',
  onCancel: () => {
    cancel('Operation cancelled.')
    process.exit(0)
  },
})

If the callback returns never, the prompt result type could narrow to exclude the cancel symbol:

// result: boolean

If the callback returns normally, the return type should remain unchanged:

// result: boolean | symbol

Notes

This is mainly about discussing whether callback-based cancellation belongs in clack at all. If the preferred approach is to keep cancellation exclusively result-based through isCancel, this can be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions