Skip to content

Commit 0ced1f1

Browse files
jabujsholladaysindresorhus
authoredJan 8, 2021
Improve docs for ky.stop (#314)
Co-authored-by: Seth Holladay <me@seth-holladay.com> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 519d17e commit 0ced1f1

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed
 

‎index.d.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ export interface Hooks {
5050

5151
/**
5252
This hook enables you to modify the request right before retry. Ky will make no further changes to the request after this. The hook function receives an object with the normalized request and options, an error instance, and the retry count. You could, for example, modify `request.headers` here.
53-
54-
If the request received a response, it will be available at `error.response`. Be aware that some types of errors, such as network errors, inherently mean that a response was not received.
53+
54+
If the request received a response, the error will be of type `HTTPError` and the `Response` object will be available at `error.response`. Be aware that some types of errors, such as network errors, inherently mean that a response was not received. In that case, the error will not be an instance of `HTTPError`.
55+
56+
You can prevent Ky from retrying the request by throwing an error. Ky will not handle it in any way and the error will be propagated to the request initiator. The rest of the `beforeRetry` hooks will not be called in this case. Alternatively, you can return the [`ky.stop`](#ky.stop) symbol to do the same thing but without propagating an error (this has some limitations, see `ky.stop` docs for details).
5557
5658
@example
5759
```
@@ -517,15 +519,18 @@ declare const ky: {
517519
extend: (defaultOptions: Options) => typeof ky;
518520

519521
/**
520-
A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry.
521-
This will also short circuit the remaining `beforeRetry` hooks.
522-
522+
A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.
523+
524+
Note: Returning this symbol makes Ky abort and return with an `undefined` response. Be sure to check for a response before accessing any properties on it or use [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining). It is also incompatible with body methods, such as `.json()` or `.text()`, because there is no response to parse. In general, we recommend throwing an error instead of returning this symbol, as that will cause Ky to abort and then throw, which avoids these limitations.
525+
526+
A valid use-case for `ky.stop` is to prevent retries when making requests for side effects, where the returned data is not important. For example, logging client activity to the server.
527+
523528
@example
524529
```
525530
import ky from 'ky';
526531
527532
(async () => {
528-
await ky('https://example.com', {
533+
const options = {
529534
hooks: {
530535
beforeRetry: [
531536
async ({request, options, error, retryCount}) => {
@@ -536,7 +541,13 @@ declare const ky: {
536541
}
537542
]
538543
}
539-
});
544+
};
545+
546+
// Note that response will be `undefined` in case `ky.stop` is returned.
547+
const response = await ky.post('https://example.com', options);
548+
549+
// Using `.text()` or other body methods is not suppported.
550+
const text = await ky('https://example.com', options).text();
540551
})();
541552
```
542553
*/

‎readme.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ Default: `[]`
248248

249249
This hook enables you to modify the request right before retry. Ky will make no further changes to the request after this. The hook function receives an object with the normalized request and options, an error instance, and the retry count. You could, for example, modify `request.headers` here.
250250

251-
If the request received a response, it will be available at `error.response`. Be aware that some types of errors, such as network errors, inherently mean that a response was not received.
251+
If the request received a response, the error will be of type `HTTPError` and the `Response` object will be available at `error.response`. Be aware that some types of errors, such as network errors, inherently mean that a response was not received. In that case, the error will not be an instance of `HTTPError`.
252+
253+
You can prevent Ky from retrying the request by throwing an error. Ky will not handle it in any way and the error will be propagated to the request initiator. The rest of the `beforeRetry` hooks will not be called in this case. Alternatively, you can return the [`ky.stop`](#ky.stop) symbol to do the same thing but without propagating an error (this has some limitations, see `ky.stop` docs for details).
252254

253255
```js
254256
import ky from 'ky';
@@ -460,11 +462,15 @@ The error thrown when the request times out. It has a `request` property with th
460462

461463
A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.
462464

465+
Note: Returning this symbol makes Ky abort and return with an `undefined` response. Be sure to check for a response before accessing any properties on it or use [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining). It is also incompatible with body methods, such as `.json()` or `.text()`, because there is no response to parse. In general, we recommend throwing an error instead of returning this symbol, as that will cause Ky to abort and then throw, which avoids these limitations.
466+
467+
A valid use-case for `ky.stop` is to prevent retries when making requests for side effects, where the returned data is not important. For example, logging client activity to the server.
468+
463469
```js
464470
import ky from 'ky';
465471

466472
(async () => {
467-
await ky('https://example.com', {
473+
const options = {
468474
hooks: {
469475
beforeRetry: [
470476
async ({request, options, error, retryCount}) => {
@@ -475,7 +481,13 @@ import ky from 'ky';
475481
}
476482
]
477483
}
478-
});
484+
};
485+
486+
// Note that response will be `undefined` in case `ky.stop` is returned.
487+
const response = await ky.post('https://example.com', options);
488+
489+
// Using `.text()` or other body methods is not suppported.
490+
const text = await ky('https://example.com', options).text();
479491
})();
480492
```
481493

0 commit comments

Comments
 (0)
Please sign in to comment.