JSFiddle - React, Tailwind, and code Playground

JavaScript

callbackLimiter = 0;

function xhrStreaming(url, callback) {

    xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    var lastSize;

    xhr.onreadystatechange = function() {
        
        if (callbackLimiter > 12) return;
        
        callbackLimiter++;
            
        //get the newest text
        newTextReceived = xhr.responseText.substring(lastSize);
        
        if (xhr.readyState > 2) {
            lastSize = xhr.responseText.length;
            callback(xhr.readyState, newTextReceived);
        }

        if (xhr.readyState == 4) {
            // create a new request if the response is finished
            xhrStreaming(url, callback);
        }
    }

    xhr.send(null);
}

function callback(readyState, responseText) {
    
    //alert(readyState + "\n\n\n" + responseText);
    document.body.innerHTML += readyState + " " + responseText + "\n";
    
}

xhrStreaming('/echo/json', callback);