Token refresh logic failing mid-batch on queue analytics export job

Problem
The nightly queue metrics export keeps dying halfway through the run. We’re pulling analytics snapshots for about twelve hours of data across the main support queues. The initial OAuth handshake works fine. The access token just expires right around the seventh API call. We’ve got a refresh routine in place, but the batch processor doesn’t catch the failure fast enough. The worker thread keeps hammering the endpoint until the connection pool times out. Weird timing issue honestly.

Code

const getNewToken = async () => {
 const res = await axios.post('https://api.mypurecloud.com/oauth/token', {
 grant_type: 'refresh_token',
 client_id: process.env.GC_CLIENT_ID,
 client_secret: process.env.GC_CLIENT_SECRET,
 refresh_token: refreshToken
 });
 return res.data.access_token;
}

const fetchQueueData = async (queueId) => {
 const token = await getNewToken();
 return axios.get(`/api/v2/analytics/queues/${queueId}/summary`, {
 headers: { Authorization: `Bearer ${token}` }
 });
}

Error
The console spits out a 401 Unauthorized. The response body just says "errors": [{"code": "unauthorized", "message": "Invalid or expired access token"}]. The batch job logs show the refresh promise resolving, but the subsequent requests still carry the old credentials. Looks like the token variable isn’t updating across the concurrent fetch calls. The array map just keeps reusing the stale string. Memory leak maybe?

Question
What’s the standard way to intercept the 401 mid-stream and swap the token without dropping the whole batch? We’re using a simple async loop right now. Should we be wrapping the axios instance with an interceptor instead? The Pacific org latency seems to make the race condition worse.