Skip to content
Foxipa
Code Completion 5 Challenges

Complete the code: The Core API

Fill in the missing pieces of the sendBeacon signature, boolean contract, and realm checks.

Beginner ~4 min read

Challenge 1

Send a fire-and-forget payload with the classic API.

js Your answer
const queued = navigator.('/api/collect', JSON.stringify(payload));

Press Ctrl⌘Enter to check your answer

Challenge 2

React correctly when the queue refuses the beacon.

js Your answer
if (!navigator.sendBeacon(url, body)) {
  // only fires on quota overflow at queueing time
  (payload); // stash for the next page load
}

Press Ctrl⌘Enter to check your answer

Challenge 3

Detect the realm where sendBeacon does not exist.

js Your answer
export function inWindowContext() {
  return typeof  !== 'undefined';
}

Press Ctrl⌘Enter to check your answer

Challenge 4

Complete the worker-context substitute for sendBeacon.

js Your answer
export function workerBeacon(url, data) {
  return fetch(url, {
    method: 'POST',
    body: data,
    : true,
  }).then(() => true).catch(() => false);
}

Press Ctrl⌘Enter to check your answer

Challenge 5

Which argument value makes sendBeacon throw TypeError immediately?

js Your answer
// throws TypeError synchronously:
navigator.sendBeacon(, body);

Press Ctrl⌘Enter to check your answer