Skip to content

Documentation / @andrew_l/toolkit / fastIdle

Function: fastIdle()

fastIdle(callback): void

Executes the provided callback as soon as the event loop is idle. This function allows you to run tasks at the earliest available opportunity without blocking the main execution flow, making it ideal for tasks that can be deferred until the browser is idle or the process is idle.

It uses requestIdleCallback if available, otherwise it falls back to requestAnimationFrame, process.nextTick, or setTimeout depending on the environment.

Parameters

callback: Fn

The callback function to be executed when the event loop is idle.

Returns

void

Example

ts
fastIdle(() => {
  console.log('1');
});

console.log('2');

// Output:
// 2
// 1