files/en-us/web/api/tasksignal/any_static/index.md
{{APIRef("Prioritized Task Scheduling API")}}{{AvailableInWorkers}}
The TaskSignal.any() static method takes an iterable of {{domxref("AbortSignal")}} objects and returns a {{domxref("TaskSignal")}}. The returned task signal is aborted when any of the abort signals is aborted.
When the task signal is aborted, its {{domxref("AbortSignal.reason", "reason")}} property will be set to the reason of the first signal that is aborted.
TaskSignal.any(signals)
TaskSignal.any(signals, init)
signals
init {{optional_inline}}
priority {{optional_inline}}
user-blocking, user-visible and background.A TaskSignal instance. It will be aborted when the first signal passed into signals is aborted. When this happens:
Its {{domxref("AbortSignal.reason", "reason")}} property will be set to the reason of the signal that caused this signal to abort.
Its {{domxref("TaskSignal.priority", "priority")}} property will be determined by the priority parameter:
priority parameter was a string, it will be the value of the string.priority parameter was a TaskSignal, it will be the value of that signal's {{domxref("TaskSignal/priority","priority")}}.TaskSignal.any()This example demonstrates combining both a signal from a {{domxref("TaskController")}}, and a timeout signal from {{domxref("AbortSignal/timeout_static", "TaskSignal.timeout()")}}.
const cancelDownloadButton = document.getElementById("cancelDownloadButton");
const userCancelController = new TaskController({
priority: "user-visible",
});
cancelDownloadButton.addEventListener("click", () => {
userCancelController.abort();
});
// Timeout after 5 minutes
const timeoutSignal = TaskSignal.timeout(1_000 * 60 * 5);
// This signal will abort when either the user clicks the cancel button or 5 minutes is up whichever is sooner
const combinedSignal = TaskSignal.any([
userCancelController.signal,
timeoutSignal,
]);
try {
const res = await fetch(someUrlToDownload, {
// Stop the fetch when any of the
signal: combinedSignal,
});
const body = await res.blob();
// Do something with downloaded content
// …
} catch (e) {
if (e.name === "AbortError") {
// Cancelled by the user
} else if (e.name === "TimeoutError") {
// Show user that download timed out
} else {
// Other error, e.g. network error
}
}
{{Specifications}}
{{Compat}}