Offload tasks to a pool of workers on node.js and in the browser
Overall
score
95%
A task execution system that processes computational tasks with built-in timeout and cancellation capabilities. The system should handle multiple concurrent tasks, provide cleanup operations, and properly handle different error scenarios.
Implement a task manager that:
The system should execute tasks in a worker pool and return results. Tasks may succeed, fail, timeout, or be cancelled.
When a task is cancelled:
When a task exceeds its timeout:
Cleanup functions should execute in all scenarios:
@generates
/**
* Create a task manager instance
*/
function createTaskManager() {
// Returns object with methods to execute and manage tasks
}
/**
* Execute multiple tasks concurrently
* @param {Array} tasks - Array of task specifications
* @returns {Promise<Array>} Results from all tasks
*/
function executeAllTasks(tasks) {
// Execute tasks and return array of results
}
/**
* Execute a task with cancellation support
* @param {string} taskName - Name of the task to execute
* @param {Array} params - Parameters for the task
* @returns {Promise} Task promise that can be cancelled
*/
function executeWithCancellation(taskName, params) {
// Returns promise with cancel() method
}
/**
* Execute a task with timeout
* @param {string} taskName - Name of the task to execute
* @param {Array} params - Parameters for the task
* @param {number} timeoutMs - Timeout in milliseconds
* @returns {Promise} Task promise that will timeout
*/
function executeWithTimeout(taskName, params, timeoutMs) {
// Returns promise that rejects on timeout
}
/**
* Execute a task with cleanup
* @param {string} taskName - Name of the task to execute
* @param {Array} params - Parameters for the task
* @param {Function} cleanupFn - Function to run after task completes
* @returns {Promise} Task promise with cleanup
*/
function executeWithCleanup(taskName, params, cleanupFn) {
// Returns promise that runs cleanup regardless of outcome
}
/**
* Check if an error is a cancellation error
* @param {Error} error - Error to check
* @returns {boolean} True if cancellation error
*/
function isCancellationError(error) {
// Returns true for cancellation errors
}
/**
* Check if an error is a timeout error
* @param {Error} error - Error to check
* @returns {boolean} True if timeout error
*/
function isTimeoutError(error) {
// Returns true for timeout errors
}
module.exports = {
createTaskManager,
executeAllTasks,
executeWithCancellation,
executeWithTimeout,
executeWithCleanup,
isCancellationError,
isTimeoutError
};Provides worker pool functionality for task execution with advanced promise features including cancellation, timeouts, and enhanced error handling.
Install with Tessl CLI
npx tessl i tessl/npm-workerpoolevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10