Skip to content

Commit 1086b80

Browse files
committed
better documentation
1 parent ef6b348 commit 1086b80

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ fuzz.extractAsPromised("gonna get canceled", choices, options)
9494
cancelToken.canceled = true;
9595
9696
// or use AbortController to cancel search
97-
98-
// Cancel search
9997
const abortController = new AbortController();
10098
10199
options.abortController = abortController;
@@ -332,12 +330,41 @@ results = fuzz.extract(query, choices, options);
332330
333331
### Async and Cancellation
334332
335-
When using extractAsPromised or extractAsync, create a new object with a 'canceled' property to use as a cancel token. For performance, by default only every 256th loop will be async, but set asyncLoopOffset to change. It is most likely not worth changing this.
333+
When using extractAsPromised or extractAsync, you might want to cancel the action before it has finished.
334+
It can be done using `CancelToken` or `AbortController`.
335+
336+
For performance, by default only every 256th loop will be async, but set `asyncLoopOffset` to change. It is most likely not worth changing this.
337+
338+
**AbortController**
339+
340+
[AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) is present in modern browsers and from node version `15+`. It is currently a standard way how to cancel running operations.
336341
337342
```js
338-
let cancelToken = {canceled: false};
343+
// or use AbortController to cancel search
344+
const abortController = new AbortController();
345+
346+
options.abortController = abortController;
347+
options.asyncLoopOffset = 64;
348+
349+
fuzz.extractAsPromised("gonna get aborted", choices, options)
350+
.then(res => {/* do stuff */})
351+
.catch((e) => {
352+
if (e.message === 'aborted') console.log('I got aborted!')
353+
});
354+
355+
abortController.abort();
356+
```
357+
358+
**CancelToken**
359+
360+
For older browsers and node versions you can use cancel token. It is an in
361+
362+
```js
363+
let cancelToken = { canceled: false };
364+
339365
options.cancelToken = cancelToken;
340366
options.asyncLoopOffset = 64;
367+
341368
fuzz.extractAsPromised("gonna get canceled", choices, options)
342369
.then(res => {/* do stuff */})
343370
.catch((e) => {

0 commit comments

Comments
 (0)