Creates a cancellable asynchronous task. Returns a CancellableHandle that can be used to cancel the task or await its result.
The type of the task result
A CancellableHandle for managing the task
const handle = cancellable(async (token) => {
await token.sleep(1000);
await token.wrap(fetch('/api/xxx', { signal: token.signal }))
await token.wrap(cancellable(async token => {
await token.wrap(new Promise(resolve => {
setTimeout(resolve, 1000);
}))
}))
return "done";
});
// Cancel the task
handle.cancel("User cancelled");
Creates a cancellable asynchronous task with options.
Errors will be rejected unless fallback is provided.
The type of the task result
The async function to execute, receives a CancellableToken
Optionaloptions: CancellableOptions<T>Configuration options including retry, timeout and fallback
A CancellableHandle for managing the task
Core implementation of the cancellable task creator.
Template: T
The type of the task result
Param: asyncTask
The async function to execute, receives a CancellableToken
Param: options
Full configuration options including retry logic and fallback behavior
Returns
A CancellableHandle that resolves to T