Stream text to speech in chunks but my chunks are coming to fast

Im using with stream: true to create a streamed response from the chat. It outputs chunks which I am sending to my textToSpeech function. This function does create a audio but the chunks are all in a mess.

In python I created the same solution but I used something like this to handle the chunks:

        if (text_chunk := chunk.choices[0].delta.content) is not None:
            yield text_chunk
    response = ' '.join(word for word in tempResponse if word)
    response = re.sub(r'\s+', ' ', response)

and then I took one chunk at the time and generated the audio.

But when Im using stream in javascript all the audio chunks seems to come all at once.

How can I make them align so they are playing sorted and only one chunk at the time

async function chat(response) {
  const completion = await{
    model: "gpt-3.5-turbo",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: response },
    stream: true,

  for await (const chunk of completion) {

// text to speech
async function textToSpeech(text) {
  if (!text) {
    console.error("Error: Text is undefined or empty.");
  try {
    const response = await
        model: "tts-1",
        input: text,
        voice: "alloy",
        headers: {
          Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
          "Content-Type": "application/json",
        responseType: "blob", // Specify the response type as arraybuffer

    // Create a Blob object from the response data
    const blob = new Blob([], { type: "audio/mpeg" });

    // Create a temporary URL for the Blob object
    const url = window.URL.createObjectURL(blob);

    // Create an audio element and set its source to the Blob URL
    const audio = new Audio(url);

    // Play the audio;

    // Cleanup: Revoke the temporary URL when audio playback ends
    audio.addEventListener("ended", () => {

    console.log("Speech generated successfully.");
  } catch (error) {
    console.error("Errora generating speech:", error);

try making a catch and for the chunks try making a hierarchy for the chunks so the first is first and the last is last not all of them at once just have to do some fine tuning to this should be good