OAuth token refresh race condition in Node.js batch job

Running a batch script to export interaction data via the Genesys Cloud API. The job loops through thousands of records, but it crashes halfway through when the access token expires. I’m using the genesyscloud npm package for auth, but the internal refresh logic isn’t catching the 401s fast enough before the next request fires.

The error is pretty standard:

Error: Request failed with status code 401
Response: { "code": "invalidToken", "message": "Access token expired" }

I’ve tried wrapping the API calls in a retry handler that checks for 401 and forces a manual client.refreshToken(), but the OTel spans get messed up because the context doesn’t propagate cleanly across the async refresh boundary. The trace shows a break where the token renewal happens, making it hard to correlate the failure with the original Data Action trigger.

Is there a better pattern for handling token expiry in a high-volume loop? Ideally, I want the SDK to handle the refresh transparently without breaking the OpenTelemetry context chain. Current setup uses Node 18 and the latest SDK version.