The Aurelia 2 Task Queue is a robust scheduler designed to address various challenges associated with timing issues, memory leaks, race conditions, etc. Unlike its predecessor in Aurelia 1, the Task Queue offers advanced control over synchronous and asynchronous tasks.
Comparison with Aurelia 1
While the term “task queue” may be familiar to those who have worked with Aurelia 1, it’s essential to recognise the fundamental differences in Aurelia 2. The new Task Queue is designed to prevent common challenges and offers enhanced capabilities and flexibility. In Aurelia 1, the task queue was notoriously known as something you used to execute code after rendering changes.
Advantages over Native Methods
The Task Queue provides precise control over task scheduling and execution, enabling a more streamlined and error-free development process and eliminating the need for native functions like setTimeout
and setInterval
.
Task Queue API
Scheduling a Task (Synchronous)
Tasks can be scheduled with specific delays and other configurations. Here’s an example:
import { PLATFORM } from 'aurelia'; const task = PLATFORM.taskQueue.queueTask(() => { console.log('Task executed after 100ms delay'); }, { delay: 100 }); // Optional: Cancel the task task.cancel();
Asynchronous Task Execution
The task queue can handle promises and async functions for asynchronous operations:
import { PLATFORM } from 'aurelia'; const asyncTask = async () => { await fetchData(); console.log('Data fetched successfully'); }; PLATFORM.taskQueue.queueTask(asyncTask);
Configuration Options
Tasks can be configured with various options:
- Priority: You can assign different priorities to tasks. Higher-priority tasks will be executed before lower-priority ones.
- Reusable: A reusable task can be re-queued after completion. This is useful for recurring tasks that need to be executed multiple times.
Practical Use Cases and Examples
Replacing setTimeout
and setInterval
Scheduling a Task with a Delay
You can schedule a task to be executed after a specific delay:
const delayedTask = () => { console.log('This task is executed after a 100ms delay'); }; PLATFORM.taskQueue.queueTask(delayedTask, { delay: 100 });
Cancelling a Delayed Task
Scheduled tasks can be cancelled using the task.cancel()
method.
Handling Asynchronous Operations
Fetching Data from an API
Here’s an example of fetching data from an API using the task queue:
const fetchData = async () => { const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log('Data fetched successfully:', data); }; PLATFORM.taskQueue.queueTask(fetchData);
Complex Task Scheduling
Priority and Reusability
You can set priorities and reusability for tasks:
const highPriorityTask = () => { console.log('High priority task executed'); }; PLATFORM.taskQueue.queueTask(highPriorityTask, { priority: 1, reusable: true });
Conclusion
The Aurelia 2 Task Queue is a powerful feature distinguishing itself from its Aurelia 1 counterpart. Embracing synchronous and asynchronous tasks provides developers a unified and efficient way to manage task execution. This article has demonstrated various practical examples, showcasing how to replace native methods and handle complex scheduling needs.
By understanding and leveraging the capabilities of the Aurelia 2 Task Queue, developers can create more robust, efficient, and maintainable applications. Best of all, no setTimeout hacks are needed.