You could also add this to the while loop after the break
// This await new promise function is when the code checks a condition repeatedly,
// pausing for 1 second between each check until the condition is met
await new Promise(resolve => setTimeout(resolve, 1000)) // Simple 1-second poll