async function pollRequest(requestId, token, opts = {}) {
const {
maxAttempts = 90,
interval = 60000, // 1 minute between each poll
} = opts;
for (let i = 0; i < maxAttempts; i++) {
const res = await fetch(
`https://api.wokelo.ai/api/enterprise/request/status/?request_id=${requestId}`,
{ headers: { Authorization: `Bearer ${token}` } }
);
const { status } = await res.json();
if (status === "COMPLETED") return fetchResult(requestId);
if (status === "FAILED") throw new Error("Request failed");
await new Promise(r => setTimeout(r, interval));
}
// Timed out after 90 minutes — our team will be notified and look into it.
throw new Error(
"Polling timed out after 90 minutes. Our team has been notified and will investigate."
);
}