I think you have to check if run status is complete (or failed), for example
let runComplete = false;
while (!runComplete) {
const runStatus = await openai.beta.threads.runs.retrieve(
thread.id,
run.id
);
runComplete = runStatus.status === "completed" || runStatus.status === "failed";
}
console.log("runComplete");
const messages = await openai.beta.threads.messages.list(
thread.id,
{}
);