fetch-stream
v0.6.5
Published
Easy fetch of HTTP/1.1 chunked content.
Downloads
11
Readme
fetch-stream
Easy fetch of HTTP/1.1 chunked content.
Basic usage
import fetchStream from 'fetch-stream';
const handler = (result) => {
if (result.done) {
console.log('completed');
return;
}
console.log(result.value);
return i < 100; // return false to cancel
};
fetchStream('/api/stream', handler);
Usage of stream API
import fetchStream from 'fetch-stream';
const handler = (result) => {
if (result.done) {
console.log('completed');
return;
}
console.log(result.value);
return i < 100; // return false to cancel
};
const stream = fetchStream('/api/stream');
const pump = () => {
stream.read().then((result) => {
if (result.done) {
return;
}
if (handler(result) === false) {
stream.cancel();
return;
}
pump();
});
};
// process all chunks
pump();