Analysis · JavaScript
Read running jobs with bounded polling
Handle asynchronous job states without unbounded requests or automatic repeated spending.
Before you begin
- A jobId returned by an instrument or chart request.
- The same server credential context that created the job.
Implementation
- 1.Poll status using a bounded interval and a finite attempt count.
- 2.Stop on succeeded or failed.
- 3.Allow the operator to resume status lookup after timeout, not start a new paid analysis.
- 4.Avoid logging result bodies or credentials.
poll-job.js
export async function waitForJob(brokerApiKey, jobId, signal) {
for (let attempt = 0; attempt < 12; attempt++) {
if (signal?.aborted) throw new Error('Polling cancelled');
const response = await fetch(
'https://tradeeyes.app/api/public/business/v1/status?id=' + encodeURIComponent(jobId),
{ headers: { Authorization: 'Bearer ' + brokerApiKey }, signal }
);
if (!response.ok) throw new Error('Status lookup failed: ' + response.status);
const job = await response.json();
if (job.status === 'succeeded') return job;
if (job.status === 'failed') throw new Error('Job failed; review before retrying');
await new Promise(resolve => setTimeout(resolve, Math.min(1000 * (attempt + 1), 5000)));
}
throw new Error('Polling timed out; resume lookup with the existing job ID');
}Validation & limitations
- The sample polling schedule is a client strategy, not an API latency guarantee.
- Widget tokens can only read jobs within their own session.
- Network retry and a new paid analysis are separate actions.