TaskSignal: phương thức tĩnh any()
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Note: This feature is available in Web Workers.
Phương thức tĩnh TaskSignal.any() nhận một iterable các đối tượng AbortSignal và trả về một TaskSignal. Tín hiệu tác vụ được trả về bị hủy khi bất kỳ tín hiệu hủy nào bị hủy.
Khi tín hiệu tác vụ bị hủy, thuộc tính reason của nó sẽ được đặt thành lý do của tín hiệu đầu tiên bị hủy.
Cú pháp
js
TaskSignal.any(signals)
TaskSignal.any(signals, init)
Tham số
signalsinitOptional-
Chứa các tham số cấu hình tùy chọn. Hiện tại chỉ có một thuộc tính được định nghĩa:
priorityOptional-
Một trong các giá trị sau:
- Chuỗi ưu tiên là một trong
user-blocking,user-visiblevàbackground. - Một
TaskSignal.
- Chuỗi ưu tiên là một trong
Giá trị trả về
Một phiên bản TaskSignal. Nó sẽ bị hủy khi tín hiệu đầu tiên được truyền vào signals bị hủy. Khi điều này xảy ra:
Ví dụ
>Sử dụng TaskSignal.any()
Ví dụ này trình bày kết hợp cả tín hiệu từ TaskController và tín hiệu timeout từ TaskSignal.timeout().
js
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
}
}
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Prioritized Task Scheduling> # dom-tasksignal-any> |