In the docs there is an example using ndjsonStream with fetch (below). I’m wondering if someone could inform me how/where to cancel the stream from the front-end? For example, if I have a dropdown that calls fetch on change, I don’t see where I could use reader.cancel() when the next fetch is made.
import ndjsonStream from "can-ndjson-stream";
fetch( "/some/endpoint" ) // make a fetch request to a NDJSON stream service
.then( ( response ) => {
return ndjsonStream( response.body ); //ndjsonStream parses the response.body
} ).then( ( exampleStream ) => {
//retain access to the reader so that you can cancel it
const reader = exampleStream.getReader();
let read;
reader.read().then( read = ( result ) => {
if ( result.done ) {
return;
}
console.log( result.value ); //logs {item:"first"}
exampleStream.getReader().read().then( read );
} );
} );