Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement a timeout task modifier? #581

Open
lozjackson opened this issue Jan 7, 2025 · 0 comments
Open

How to implement a timeout task modifier? #581

lozjackson opened this issue Jan 7, 2025 · 0 comments

Comments

@lozjackson
Copy link

lozjackson commented Jan 7, 2025

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.

@lozjackson lozjackson changed the title How to implement a timeout task modifier How to implement a timeout task modifier? Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant