task-invoker
A tiny TypeScript/JavaScript utility for scheduling asynchronous task execution with zero delay. Perfect for deferring function calls to the event loop without using setTimeout(fn, 0).
✨ Features
- 🌀 Run tasks asynchronously with zero delay
- ⚡ Lightweight and dependency-free
- 🛡️ Written in TypeScript with type definitions
📦 Installation
npm install task-invoker
# or
yarn add task-invoker
# or
pnpm add task-invoker
🚀 Usage
import { schedule } from 'task-invoker';
const foo = () => {
console.log('Hello from async task!');
};
// Task will be executed asynchronously, with no artificial delay
schedule(foo);
console.log('This will log first.');
🛠️ How it Works
task-invoker
leverages microtask or macrotask scheduling to queue your function for execution after the current call stack clears — without introducing unnecessary delays.
This means:
- No need for
setTimeout(fn, 0)
- Works consistently across modern runtimes
- Ensures smoother async flows
📚 API
schedule(fn: (...args: any[]) => void): void
Schedules a function to run asynchronously.
fn
— function to execute
📖 Examples
Debouncing heavy tasks
import { schedule } from 'task-invoker';
window.addEventListener('resize', () => {
schedule(() => {
console.log('Window resized, handle expensive logic here');
});
});
Async UI updates
schedule(() => {
document.body.classList.add('loaded');
});
🔗 Links
📜 License
MIT © Anatolii Parubets