Skip to content

Commit

Permalink
fix: moved async code in examples to top-level-await
Browse files Browse the repository at this point in the history
  • Loading branch information
mainarthur authored and ronag committed Jun 12, 2022
1 parent e00bd1d commit 30a464b
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions README.md
Expand Up @@ -187,11 +187,10 @@ Basic usage example:
```js
import { fetch } from 'undici';

async function fetchJson() {
const res = await fetch('https://example.com')
const json = await res.json()
console.log(json);
}

const res = await fetch('https://example.com')
const json = await res.json()
console.log(json);
```

You can pass an optional dispatcher to `fetch` as:
Expand Down Expand Up @@ -235,24 +234,20 @@ const data = {
},
};

(async () => {
await fetch("https://example.com", { body: data, method: 'POST' });
})();
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()`.

```js
import {fetch} from 'undici';
import {Readable} from 'node:stream';
import { fetch } from 'undici';
import { Readable } from 'node:stream';

async function fetchStream() {
const response = await fetch('https://example.com')
const readableWebStream = response.body;
const readableNodeStream = Readable.fromWeb(readableWebStream);
}
const response = await fetch('https://example.com')
const readableWebStream = response.body;
const readableNodeStream = Readable.fromWeb(readableWebStream);
```

#### Specification Compliance
Expand Down

0 comments on commit 30a464b

Please sign in to comment.