-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add abort controller (#216)
Co-authored-by: jacobparis <[email protected]> Co-authored-by: Nate Moore <[email protected]> Co-authored-by: Nate Moore <[email protected]>
- Loading branch information
1 parent
4c98bd2
commit 801246b
Showing
3 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
"@clack/core": minor | ||
"@clack/prompts": minor | ||
--- | ||
|
||
Adds a new `signal` option to support programmatic prompt cancellation with an [abort controller](https://kettanaito.com/blog/dont-sleep-on-abort-controller). | ||
|
||
One example use case is automatically cancelling a prompt after a timeout. | ||
|
||
```ts | ||
const shouldContinue = await confirm({ | ||
message: 'This message will self destruct in 5 seconds', | ||
signal: AbortSignal.timeout(5000), | ||
}); | ||
``` | ||
|
||
Another use case is racing a long running task with a manual prompt. | ||
|
||
```ts | ||
const abortController = new AbortController() | ||
|
||
const projectType = await Promise.race([ | ||
detectProjectType({ | ||
signal: abortController.signal | ||
}), | ||
select({ | ||
message: 'Pick a project type.', | ||
options: [ | ||
{ value: 'ts', label: 'TypeScript' }, | ||
{ value: 'js', label: 'JavaScript' }, | ||
{ value: 'coffee', label: 'CoffeeScript', hint: 'oh no'}, | ||
], | ||
signal: abortController.signal, | ||
}) | ||
]) | ||
|
||
abortController.abort() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters