-\item Next we will fetch data from the REAS-server. Use the \code{fetch(url, [options])} function to send a http request to the inventory server. It might be easiest to build the url using string concatenation, but you can also check out the \code{URL(url, [base])} class. Browse the documentation of \code{fetch()}. It returns a \code{Promise} that resolves to a \code{Response} object. To get the http body you can use \code{Response.prototype.json()}, which returns yet another \code{Promise}. First fetch the list of foundations, then for each foundation fetch its properties from the server, e.g. \code{fetch(‘http://localhost:8080/extras/Tomat’)}. Fetching the properties of the ingredients are independent actions and to reduce loading time, you must fetch them concurrently. \emph{Hint:} \code{fetch()} returns directly, so it is easy to start several concurrent requests. Use \code{Promise.all()} when you need to wait for several promises. Important! The \code{fetch()} promise will only reject with a TypeError if a network error occurs. If the server response with an http error code, for example 404, \code{fetch()} will treat this as a valid response. To catch http errors you need to check the response.ok flag:
0 commit comments