Skip to content

How to implement a timeout task modifier? #581

Description

@lozjackson

I would like to implement a task modifier that, when added to a task, will make it throw an error if it takes too long.

It would be used like this:

myTask = task({timeout: 3000}, async () => {
  await someAsyncStuff
})

and if someAsyncStuff doesn't resolve or reject within the specified time it will throw a timeout error

This is what I've tried, but it doesn't seem to work.

import {race, rawTimeout} from 'ember-concurrency';

export default function timeout(taskFactory, time) {
  const taskDefinition = taskFactory.taskDefinition;

  const timer = async () => {
    await rawTimeout(time);
    throw new Error(timeout);
  };

  const timeoutTaskDefinition = function* (...args) {
    yield* race([
      taskDefinition.call(this, ...args),
      timer()
    ]);
  };

  taskFactory.setTaskDefinition(timeoutTaskDefinition);
}

// initializer
import {registerModifier} from 'ember-concurrency';
import timeoutTaskModifer from 'frontend/task-modifiers/timeout';
registerModifier('timeout', timeoutTaskModifer);

It throws this error when starting the task

Uncaught TypeError: yield* (intermediate value) is not iterable

Any help would be appreciated.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions