Retrying Concurrency Tasks

I’ve been playing around with the Premier League Fantasy Football API in the evenings recently and came across a problem with their API.

The small app I’ve built needs to make requests to a number of different end points to load data. I noticed that sometimes these requests return a 200 status but fail to return their payload!

This results in a broken app. I went searching for a solution that could detect a failed request and try again (in almost all cases the 2nd request does return it’s payload)

Ember Concurrency Retryable

Enter the ember-concurrency-retryable plugin!

import Service from '@ember/service';
import { task } from 'ember-concurrency';
import { ExponentialBackoffPolicy } from 'ember-concurrency-retryable';

const backoffPolicy = new ExponentialBackoffPolicy({
  multiplier: 1.5,
  minDelay: 30,
  maxDelay: 400,
});

export default class FplApiService extends Service {

  getLeagueData = task({ retryable: backoffPolicy }, async () => {

      const result = await axios.get(LEAGUE_DETAILS_API);

      if (!result.data) {
        throw new Error('Did not get payload');
      }

      // process the data
    }
  });

}

The above getLeagueData task now throws an error if the response did not contain valid data, this causes the task to be retryed according to the backoff policy.

Jim Wardlaw

Developer, Designer, UX Zealot. Ember & Tailwind