Skip to content

Commit

Permalink
Improve asynchronous handlers documentation (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnmkng authored Jun 26, 2021
1 parent a99262c commit 7524d90
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1543,16 +1543,17 @@ const mergedHandlers = got.extend({
});
```

**Note:** Handlers can be asynchronous. The recommended approach is:
**Note:** Handlers can be asynchronous and can return a `Promise`, but never a `Promise<Stream>`. Streams must always be handled synchronously. If you need asynchronous handlers for streams, use `beforeRequest` hooks. The recommended approach for creating handlers that can handle both promises and streams is:

```js
// Create a synchronous handler because an async function will not work with streams.
const handler = (options, next) => {
if (options.isStream) {
// It's a Stream
// It's a Stream, return synchronously.
return next(options);
}

// It's a Promise
// For asynchronous work return a Promise
return (async () => {
try {
const response = await next(options);
Expand Down

0 comments on commit 7524d90

Please sign in to comment.