-
-
Notifications
You must be signed in to change notification settings - Fork 0
MicroTask
Eugene Lazutkin edited this page May 7, 2026
·
9 revisions
The MicroTask class is a utility designed to encapsulate a function (fn) that can be executed later. It allows for the creation of tasks that can be queued for execution in the microtask queue, ensuring they run after the current operation completes but before the next rendering.
Usually MicroTask is used as a base class for other classes that need to schedule tasks for execution in the microtask queue.
- Encapsulation: Wraps a function for deferred execution.
- Microtask Scheduling: Can be used to schedule tasks that need to run immediately after the current script but before any rendering.
- Promise Support: Supports creating and resolving promises for asynchronous task completion.
MicroTask is available as a named and default export.
const task = new MicroTask(fn);-
fn: The function to be executed later. It will be called with queue-specific arguments, and its return value will be ignored byenqueue(), but it can be used byschedule()to resolve the task's promise.
-
fn: The function to be executed later. It will be called with queue-specific arguments, and its return value will be ignored byenqueue(), but it can be used byschedule()to resolve the task's promise. -
isCanceled: A boolean indicating whether the task has been canceled.
-
promise: A promise that resolves when the task is executed, ornullifmakePromise()hasn't been called yet. -
settled: A boolean indicating whether the task has been settled (resolved or rejected). -
cancelError: The error supplied to the firstcancel(error)call, ornull. Useful for inspecting why a non-promised task was canceled when there's no rejection to carry the cause.
-
makePromise(): Creates the promise lazily. Idempotent — repeat calls returnthiswithout changing state. Ifcancel()was called beforemakePromise(), the freshly-created promise is settled immediately as aCancelTaskErrorrejection, carrying any storedcancelErrorascause. Returns the task instance. -
resolve(value): Resolves the task's promise with the specified value. Returns the task instance. Throws ifmakePromise()hasn't been called — without a promise to resolve, the value would be silently dropped (and any laterawait task.makePromise().promisewould hang). -
cancel(error): Cancels the task. Always setsisCanceled = trueso queues skip the task. IfmakePromise()has been called, also rejects the promise with aCancelTaskError. The firsterrorpassed is stored on the instance and accessible viacancelError; ifcancel(error)runs beforemakePromise(), the stored error is replayed when the promise is later created. Returns the task instance.
To use the MicroTask class, import it into your project and create an instance with the desired function:
import MicroTask from 'time-queues/MicroTask.js';
const logTask = new MicroTask(() => console.log('Task executed!'));
// Execute the task
logTask.fn(); // Will log 'Task executed!'
logTask.promise; // null
logTask.isCanceled; // false-
Function Not Executing: Ensure that the function passed to
MicroTaskis defined and can be executed without errors. - Microtask Queue Issues: If tasks are not executing as expected, ensure that the microtask queue is being processed correctly in your environment.
-
resolve()throws: CallmakePromise()first, or useschedule()(which makes the promise eagerly). -
cancel(error)reason lost: If you cancel a task without a promise and inspect later, readtask.cancelError. If you callmakePromise()after cancellation, the stored error is replayed ascauseon the rejection automatically.
- MicroTaskQueue - Queue that manages MicroTasks
- CancelTaskError - Error used when canceling tasks