Nano-Task management library
npm install queue-nano-taskqueue-nano-task is a lightweight JavaScript library designed to manage and schedule nanotasks efficiently.
Inspired by queueMicrotask, it provides a controlled, internal flow of tasks that allows fine-grained task execution management.


Using npm:
``bash`
npm i queue-nano-task
Using yarn:
`bash`
yarn add queue-nano-task
The main queueNanotask function accepts one required parameter and two optional:
- The first parameter is a task represented by a function that will be executed within the task queue.
- The second (optional) parameter sets the task priority, with a default value of 0.
- The third (optional) parameter determines the queue order, where LIFO is used if true; otherwise, FIFO by default.
If queueNanotask is executed outside the context of another queueNanotask call, the code is executed immediately during the call.
`typescript
import { queueNanotask } from 'queue-nano-task'
const log: any[] = []
queueNanotask(() => log.push(42))
// log: [42]
`
Control the execution priority of Nano-Tasks with the second and the third optional argument.
- The third argument, when true, switches the order of the queue from the default FIFO to LIFO.
- Lower priority values are executed before higher priority values.
- Tasks with the same priority are ordered based on FIFO or LIFO, depending on the third parameter.
`typescript
import { queueNanotask, Task } from 'queue-nano-task'
const log: any[] = []
const logger = (value: any): Task => () => {
log.push(value)
}
queueNanotask(() => {
queueNanotask(logger('Mounted'), 2)
queueNanotask(logger('Mounting'), 1)
queueNanotask(logger('Rendering'), 0)
queueNanotask(logger('WillMount'), 1, true)
})
// log: ['Rendering', 'WillMount', 'Mounting', 'Mounted']
`
In the example, 'Rendering' (priority 0) executes first, followed by 'WillMount' (priority 1, LIFO), then 'Mounting' (priority 1, FIFO), and finally 'Mounted' (priority 2).
> Runs from the left to the right
> [true, ..., false] > [true, ..., false] > ... ^ 0 ^ ^ 1 ^`
>
If you find bugs or want to suggest features, please open an issue on GitHub.
