Fetching from the Network
Ask a server for data with fetch, read the Response as JSON, text
or bytes, stop a request with an AbortController or a timeout,
and tell a failed request from a reply that is not ok.
After reading this page, you will know how a script asks a server for data with
fetch, how it reads the reply, how it stops a request or gives it a deadline, and what an error is.A script fetches as a web page does:
fetch, Request, Response, Headers, AbortController and AbortSignal, with the types and the meaning WHATWG gives them. The request runs on a thread of the platform, and the application does nothing while it waits: no timer and no frame. Its reply wakes the application, settles the promise and runs what waited on it.fetch takes an address and gives a promise of a Response. The body is read once, as text, JSON or bytes, each a promise of its own. What json() gives is unknown: the script narrows it before it reads a member. This example reads activity.json, a file beside it:On ljos.dev, and in a browser, a relative address is read against the page, so the example shows "Walk to the lighthouse". On the desktop an address names a server,
https://…: a relative one is not an address there, so the same example shows the message of the TypeError that fetch rejects with.A status the server sends,
404 or 500, is a Response whose ok is false: fetch does not reject for it. text() gives the body as a string and arrayBuffer() as bytes; reading a body a second time rejects with a TypeError, and bodyUsed says it was read.The second argument says what is sent:
method, headers and body. Headers are a Headers, an object of names or a list of pairs; a name is kept once, in lower case, and the values given for it are joined with , . Headers the platform owns, such as Connection or Content-Length, are not sent. A body of text is sent as text/plain;charset=UTF-8 unless the headers say otherwise, and a Uint8Array or an ArrayBuffer is sent as it is.A
Request holds the same two arguments, and fetch(request) sends it. This example makes one and shows what it would send, without sending it:Its first line is
POST and the address: names on the desktop, which keeps the address as given, and in a browser the whole address names makes beside the page. The second reads content-type: application/json: the header given wins over the one the body would set.An
AbortController's signal, given as signal, stops the request when the controller aborts: the fetch rejects with the signal's reason, a DOMException named AbortError unless the script gave its own. AbortSignal.timeout(ms) is a signal that aborts by itself after ms milliseconds, with a TimeoutError, and AbortSignal.any([...]) aborts with the first of several.The example asks for
slow.json, a file beside it:The user's controller aborts as soon as the request is made, so the fetch rejects with an
AbortError and the text reads "stopped". The catch asks the error's name, not signal.aborted, so a request that could not be made reads "unreachable" even after an abort.fetch rejects in two cases only:the request could not be made: the server cannot be reached, the address is not one, or the method is not one the platform sends. It rejects with a
TypeError whose message begins fetch failed:.the signal aborted: it rejects with the signal's reason.
Everything the server answers is a
Response. Response.error() is a reply of no request, whose status is 0 and whose body is empty: fetch(url).catch(() => Response.error()) gives every request a Response, which is what ljos migrate writes for XMLHttpRequest.An app built for the web fetches with the browser's own
fetch, which has every name on this page with the same meaning, so a script that checks runs the same there. Two things are the browser's: a request to another origin needs the server's leave (CORS), which the desktop does not ask for, and the browser leaves out a few more headers, such as Host, than it lets a page set.A file of Ljós's API has no
XMLHttpRequest: ljos check says `XMLHttpRequest` is not in Ljós's API: use `fetch` [ts-forbidden]. A file written against Qt's API keeps it, and ljos migrate writes its usual shape as one fetch: a request that is opened, given headers, answered in an onreadystatechange that waits for readyState 4, and sent.becomes
ljos migrate --write takes out the lines the statements the call gathers leave empty; --compat lower keeps them, so that its findings point at the file as written. Any other shape, a request that waits for its reply or a handler that reads this, keeps the file, and ljos migrate says what of it has no fetch.




