Node.js WebSocket reconnect loop failing on 401 with Notification API

Getting a 401 Unauthorized immediately after the socket drops. The reconnection logic in my Node.js script is timing out before the OAuth token refresh completes, so the new connection attempt uses the expired token. Here is the reconnect handler:

ws.on('close', (code, reason) => {
 console.log('Connection lost', code);
 setTimeout(() => {
 connectToGenesys(); // Calls auth then new WebSocket
 }, 5000);
});

The auth call takes about 2 seconds but the WebSocket constructor fires right away. How do I chain the token refresh before attempting the new socket connection?