thunk-workers
Thunk-based task scheduler that executes synchrounous and/or asynchronous tasks under concurrency control.
thunks
Demo
const thunk = const thunkWorkers = const workshop = for let i = 1; i <= 10; i++ { { console }} // Result:// Task 1 start:// Task 2 start:// 121212121212121212// Task 2 finished, 510 ms.// Task 3 start:// 13131313131313131// Task 1 finished, 981 ms.// Task 4 start:// 343434343434343434343434343// Task 3 finished, 1199 ms.// Task 5 start:// 45454545454// Task 4 finished, 1031 ms.// Task 6 start:// 565656565656565656// Task 6 finished, 485 ms.// Task 7 start:// 5757575757// Task 5 finished, 1072 ms.// Task 8 start:// 78787878787// Task 8 finished, 317 ms.// Task 9 start:// 7979797979// Task 7 finished, 886 ms.// Task 10 start:// Task 9 finished, 284 ms.// Task 10 finished, 10 ms.
API
const thunkWorkers =
thunkWorkers([count])
Create a workshop that limits the number of concurrent tasks being executed.
count
: {Number} Maximum number of task threads being executed concurrently. Default to1
.
const workshop =
workshop(task)
Return a thunk function that executes a specific task. Tasks are queued by the time the returned thunk function is executed. Once the number of concurrent tasks is within workshop's limitation, a task is polled from the queue and executed.
task
: {Function} Support sync task or async task, task must be a function or a generator function. Async task should be thunkable function, or return a thunkable value, such as thunk function, promise, generator function, generator object.
// Support thunk functionconst job = // Support Promise { console} // Support Generator function { console} //Support async/await function { console}