Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
// noprotect
async function streamingRead() {
  const response = await fetch('https://html.spec.whatwg.org');
  const find = 'J';
  const findCode = find.codePointAt(0);
  let bytes = 0;
  for await (const chunk of streamAsyncIterator(response.body)) {
    const index = chunk.indexOf(findCode);
    
    if (index != -1) {
      bytes += index;
      console.log(`Found ${find} at byte ${bytes}.`);
      break;
    }
    
    bytes += chunk.length;
  }
  
  response.body.cancel();
}
async function* streamAsyncIterator(stream) {
  // Get a lock on the stream
  const reader = stream.getReader();
  try {
    while (true) {
      // Read from the stream
      const {done, value} = await reader.read();
      // Exit if we're done
      if (done) return;
      // Else yield the chunk
      yield value;
    }
  }
  finally {
    reader.releaseLock();
  }
}
streamingRead();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jakearchibaldpro
0viewers