Skip to content

Commit 9a2981c

Browse files
committed
minor fixes
1 parent c9063dc commit 9a2981c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

4-binary/03-blob/article.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -219,30 +219,34 @@ const bufferPromise = await blob.arrayBuffer();
219219

220220
// or
221221
blob.arrayBuffer().then(buffer => /* process the ArrayBuffer */);
222-
223222
```
224223

225-
`FileReader.readAsArrayBuffer()` method can also get ArrayBuffer, but it usually fails if the blob memory exceeds 2G. It is recommended to use blob.arrayBuffer(), it's simpler.
226-
227224
## From Blob to stream
228225

229-
When we read and write to a blob of more than `2G`, the use of `arrayBuffer` becomes more memory intensive for us. At this point, we can directly convert the blob to a `stream`,The `Blob` interface's `stream()` method returns a `ReadableStream` which upon reading returns the data contained within the `Blob`.
226+
When we read and write to a blob of more than `2G`, the use of `arrayBuffer` becomes more memory intensive for us. At this point, we can directly convert the blob to a stream.
230227

231-
```js
228+
A stream is a special object that allows to read from it (or write into it) portion by portion. It's outside of our scope here, but here's an example, and you can read more at <https://developer.mozilla.org/en-US/docs/Web/API/Streams_API>.
229+
230+
The `Blob` interface's `stream()` method returns a `ReadableStream` which upon reading returns the data contained within the `Blob`.
232231

232+
Then we can read from it, like this:
233+
234+
```js
233235
// get readableStream from blob
234236
const readableStream = blob.stream();
235-
const reader = readableStream.getReader();
237+
const stream = readableStream.getReader();
236238

237239
while (true) {
238-
// value => blob fragments
239-
let { done, value } = await reader.read();
240+
// for each iteration: data is the next blob fragment
241+
let { done, data } = await stream.read();
240242
if (done) {
243+
// no more data in the stream
241244
console.log('write done.');
242245
break;
243246
}
244-
// to do sth
245-
stream.write(value);
247+
248+
// do something with the data portion we've just read from the blob
249+
console.log(data);
246250
}
247251
```
248252

0 commit comments

Comments
 (0)