Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
Response.body(ReadableStream) getReader().read() is taking plenty of time for receiving first chunk as if it's waiting for entire response to complete

Currently, I am consuming a streaming API(transfer-encoding: 'chunked') and using the data in each chunk to render the UI as and when new chunk is received. When I check the timings tab under network call in my browser TTFB is around 300 ms but when I check the time to receive my first chunk, it comes more than 3000 ms though time to receive subsequent chunks is quite less (~2-3 ms) which seems like all the chunks are waiting in the internal queue. What could be the reason for this high latency for first chunk or what's wrong in the implementation below?

let requestOptions = {
    method: 'GET',
    headers,
};

fetch(`api url`, requestOptions)
    .then(processChunkedResponse)
    .then(processStream)
    .then(onChunkedResponseComplete)
    .catch(onChunkedResponseError)
    ;

function onChunkedResponseComplete(result) {
    console.log("streaming completed")
}

function onChunkedResponseError(err) {
    // handle error
}

function processChunkedResponse(response) {
    let reader = response.body.getReader();
    return new ReadableStream({
        start(controller) {
            function push() {
                reader.read().then(({ done, value }) => {      
                    if (done) {
                        console.log('done', done);
                        controller.close();
                        return;
                    }
                    controller.enqueue(value);
                    console.log(done, value);
                    push();
                })
            }
            push();
        }
    });
}

function processStream(stream) {
    let reader = stream.getReader();

    return readChunk();

    function readChunk() {
        return reader.read().then(appendChunks);
    }

    function appendChunks(result) {
        // process chunk as Uint8Array and update the UI

        if (result.done) {
            console.log('returning')
            return result;
        } else {
            console.log('recursing')
            return readChunk();
        }
    }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda