> For the complete documentation index, see [llms.txt](https://docs.aurelia.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aurelia.io/~/revisions/gR80klCEJxq2vRaf1v0G/getting-to-know-aurelia/task-queue.md).

# Task Queue

Not to be confused with task queue in Aurelia 1, the TaskQueue is a sophisticated scheduler designed to prevent a variety of timing issues, memory leaks, race conditions and more bad things that tend to result from `setTimeout`, `setInterval`, floating promises, etc.

The benefit of using the task queue is both synchronous and asynchronous tasks are supported.

{% hint style="info" %}
We highly recommend using the task queue to replace existing uses of `setTimeout` and `setInterval` for better-performing applications.
{% endhint %}

## setTimeout (synchronous)

In the following example, we use the `delay` configuration property to configure a task with a timeout of 100 milliseconds. This would replace using `setTimeout` in your applications.

```typescript
import { PLATFORM } from 'aurelia';

// Queue
const task = PLATFORM.taskQueue.queueTask(() => {
  doStuff();
}, { delay: 100 });

// Cancel
task.cancel();
```

If you were to use a native `setTimout` it would look like this:

```typescript
// Queue
const handle = setTimeout(() => {
  doStuff();
}, 100);

// Cancel
clearTimeout(handle);
```

Now, in your unit/integration/e2e tests or other components, you can `await PLATFORM.taskQueue.yield()` to deterministically wait for the task to be done (and not a millisecond longer than needed) or even `PLATFORM.taskQueue.flush()` to immediately run all queued tasks.

Result: no more flaky tests or flaky code in general. No more intermittent and hard-to-debug failures.

## setTimeout (asynchronous)

We performed a synchronous equivalent of a setTimeout. Now we can go one step further and do an asynchronous setTimeout, minus the floating promises and memory leaks.

```typescript
import { PLATFORM } from 'aurelia';

// Queue
const task = PLATFORM.taskQueue.queueTask(async () => {
  await doAsyncStuff();
}, { delay: 100 });

// Await
await task.result;

// Cancel
task.cancel();
```

## setInterval

By supply the `persistent` configuration value, we can specify a task will remain and not be cleared by the task queue. This gives us `setInterval` functionality.

```typescript
import { PLATFORM } from 'aurelia';

// Queue
const task = PLATFORM.taskQueue.queueTask(() => {
  poll();
}, { delay: 100, persistent: true /* runs until canceled */ });

// Stop
task.cancel();
```

## requestAnimationFrame

By leveraging the `domWriteQueue` we can also replace `requestAnimationFrame` with a safer alternative.

```typescript
import { PLATFORM } from 'aurelia';

PLATFORM.domWriteQueue.queueTask(() => {
  applyStyles();
});
```

## requestAnimationFrame (loop)

In situations where `requestAnimationFrame` is being used in a loop capacity (such as high frame rate animations), we can loop. The queue greatly simplifies this again with the `persistent` configuration option we saw above.

```typescript
// Start
const task = PLATFORM.domWriteQueue.queueTask(() => {
  updateAnimationProps();
}, { persistent: true });
// Stop
task.cancel();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aurelia.io/~/revisions/gR80klCEJxq2vRaf1v0G/getting-to-know-aurelia/task-queue.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
