FetchEvent: thuộc tính handled
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2022.
Note: This feature is only available in Service Workers.
Thuộc tính handled của giao diện FetchEvent trả về một promise cho biết liệu sự kiện đã được xử lý bởi thuật toán tìm nạp hay chưa. Thuộc tính này cho phép thực thi mã sau khi trình duyệt đã xử lý phản hồi, và thường được dùng cùng với phương thức waitUntil().
Giá trị
Một Promise đang chờ trong khi sự kiện chưa được xử lý, và được thực hiện khi đã xử lý xong.
Ví dụ
js
addEventListener("fetch", (event) => {
event.respondWith(
(async function () {
const response = await doCalculateAResponse(event.request);
event.waitUntil(
(async function () {
await doSomeAsyncStuff(); // tùy chọn
// Chờ sự kiện được trình duyệt xử lý
await event.handled;
return doFinalStuff(); // Hoàn tất SAU KHI sự kiện đã được xử lý
})(),
);
return response;
})(),
);
});
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Service Workers Nightly> # dom-fetchevent-handled> |