Poll Until a Job Finishes

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Watch a background job by polling. Props: checkStatus, a function that takes no arguments and returns a Promise of the job's status as a string (such as "queued", "running", "done" or "failed"); and interval, in milliseconds (default 1000). Call checkStatus as soon as the component mounts, and again interval milliseconds after each reply arrives, so there's never more than one check at a time. Show a paragraph reading "Status: unknown" until the first reply, then "Status: " followed by the latest status. Stop checking for good once the status is "done" or "failed". While it's checking, show a "Stop checking" button that stops it too. It must also stop when it unmounts. The checks use an interval of 30.

Your code must pass

  • Checks straight away and shows the status
  • Waits for each reply before checking again
  • Stops once the job is "done"
  • Stop checking ends the polling
  • Stops polling when unmounted