|
| 1 | +# Task |
| 2 | + |
| 3 | +A Task represents an asynchronous computation. It is a wrapper around a Promise that provides additional functionality and events. It can be performed only once and it manages the state of the promise. The task has built-in cancellation mechanism. |
| 4 | + |
| 5 | +## API |
| 6 | + |
| 7 | +### Construction |
| 8 | + |
| 9 | +```ts |
| 10 | +import { createTask } from 'solid-tasks'; |
| 11 | + |
| 12 | +const task = createTask(async (signal) => { |
| 13 | + const response = await fetch('...', { signal }) |
| 14 | + return response.json(); |
| 15 | +}); |
| 16 | +``` |
| 17 | + |
| 18 | +### Properties |
| 19 | + |
| 20 | +- `value` - The result of a successful Task execution. |
| 21 | +- `error` - The reason why a Task execution was unsuccessful. |
| 22 | +- `status` - The current status of the Task. Can be: `idle`, `pending`, `fulfilled`, `rejected`, or `aborted`. |
| 23 | +- `isIdle` - A boolean indicating whether the Task is idle. |
| 24 | +- `isPending` - A boolean indicating whether the Task is pending. |
| 25 | +- `isFulfilled` - A boolean indicating whether the Task was successful. |
| 26 | +- `isRejected` - A boolean indicating whether the Task was unsuccessful. |
| 27 | +- `isSettled` - A boolean indicating whether the Task has completed (fulfilled or rejected). |
| 28 | +- `isAborted` - A boolean indicating whether the Task was cancelled. |
| 29 | +- `signal` - An AbortSignal object for interpolation with signal-supported solution. |
| 30 | + |
| 31 | +### Methods |
| 32 | + |
| 33 | +- `perform()` - Starts execution of the Task. |
| 34 | +- `abort(cancelReason)` - Aborts the Task with an optional cancel reason. |
| 35 | +- `then(onFulfilled, onRejected)` - Registers callbacks to be called when the Task is fulfilled or rejected. |
| 36 | +- `catch(onRejected)` - Registers a callback to be called when the Task is rejected. |
| 37 | +- `finally(onFinally)` - Registers a callback to be called when the Task is settled. |
| 38 | +- `addEventListener(type, listener, options)` - Registers an event listener for Task events. |
| 39 | +- `removeEventListener(type, listener, options)` - Removes an event listener for Task events. |
| 40 | + |
| 41 | +### Events |
| 42 | + |
| 43 | +- `fulfill` - Triggered when the Task is fulfilled. |
| 44 | +- `reject` - Triggered when the Task is rejected. |
| 45 | +- `abort` - Triggered when the Task is cancelled. |
0 commit comments