Skip to content

Commit

Permalink
feat: add asyn iterable section in doc (nodejs#1202)
Browse files Browse the repository at this point in the history
* feat: add asyn iterable section in doc

Issue: nodejs#991

Added a description and an exmaple of async iterables in the doc

* fix: fetch method

Co-authored-by: Khafra <[email protected]>

* fix: add missing body type in readme

Co-authored-by: Khafra <[email protected]>
  • Loading branch information
2 people authored and crysmags committed Feb 27, 2024
1 parent 1d785ec commit e5a442d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,37 @@ Basic usage example:
}
```


#### `request.body`

A body can be of the following types:

- ArrayBuffer
- ArrayBufferView
- AsyncIterables
- Blob
- Iterables
- String
- URLSearchParams
- FormData

In this implementation of fetch, ```request.body ``` now accepts ```Async Iterables```. It is not present in the [Fetch Standard.](https://fetch.spec.whatwg.org)

```js
import { fetch } from "undici";

const data = {
async *[Symbol.asyncIterator]() {
yield "hello";
yield "world";
},
};

(async () => {
await fetch("https://example.com", { body: data, method: 'POST' });
})();
```

#### `response.body`

Nodejs has two kinds of streams: [web streams](https://nodejs.org/dist/latest-v16.x/docs/api/webstreams.html) which follow the API of the WHATWG web standard found in browsers, and an older Node-specific [streams API](https://nodejs.org/api/stream.html). `response.body` returns a readable web stream. If you would prefer to work with a Node stream you can convert a web stream using `.fromWeb()`.
Expand Down

0 comments on commit e5a442d

Please sign in to comment.