Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

export async function* streamToAsyncIterable(readableStream) { const reader =…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash’s answer.
  1. User

    export async function* streamToAsyncIterable(readableStream) { const reader = readableStream.getReader(); const decoder = new TextDecoder();

    while (true) { const { value, done } = await reader.read(); if (done) break; yield decoder.decode(value); } }

  2. Qwen Assistant

    Your streamToAsyncIterable function is a solid and commonly used pattern for converting a ReadableStream into an async iterable of decoded text chunks. It works well for streaming text-based responses (like those from the Fetch API or server-sent events), especially when you want to process chunks as they arrive.

    Here's what your code does:

    export async function* streamToAsyncIterable(readableStream) {
      const reader = readableStream.getReader();
      const decoder = new TextDecoder();
    
      while (true) {
        const { value, done } = await reader.read();
        if (done) break;
        yield decoder.decode(value);
      }
    }
    
    • readableStream.getReader(): Gets a reader to pull chunks from the stream.
    • TextDecoder: Converts binary data (Uint8Array) into strings.
    • yield decoder.decode(value): Yields each decoded chunk asynchronously.

    ✅ Best Practice Notes

    1. Error Handling: Currently, this doesn't handle errors that might occur during reading. You may want to wrap the read loop in a try/catch block if you're working in an environment where errors can be thrown during reading.

    2. Final Chunk: If there's trailing bytes not fully forming a character (e.g., partial UTF-8 sequence), calling .decode() without { stream: true } could result in replacement characters (``). You can fix this by using:

      yield decoder.decode(value, { stream: true });
      

      And then finally call decoder.decode() with no arguments after the loop ends to flush any remaining buffered bytes.


    🛠️ Improved Version (with error handling + proper decoding)

    export async function* streamToAsyncIterable(readableStream) {
      const reader = readableStream.getReader();
      const decoder = new TextDecoder();
      let done = false;
    
      try {
        while (!done) {
          const { value, done: readerDone } = await reader.read();
          done = readerDone;
          if (value) {
            yield decoder.decode(value, { stream: true });
          }
        }
        // After the loop, flush any remaining bytes in the decoder
        yield decoder.decode();
      } catch (error) {
        console.error('Error reading stream:', error);
        throw error;
      } finally {
        reader.releaseLock();
      }
    }
    

    🔁 Usage Example

    You can use this generator like so:

    const response = await fetch('https://example.com/stream-endpoint');
    const stream = streamToAsyncIterable(response.body);
    
    for await (const chunk of stream) {
      console.log(chunk);
    }
    

    This will log each decoded text chunk as it arrives.


    Let me know if you'd like to support binary streams too, or convert to something like lines or JSON chunks!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.