From ad4a2036045e946c444ea5109b6edd0c968e2fef Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 8 Jan 2018 19:07:56 +0200 Subject: [PATCH] Added error handling Added handling for `onerror` to log on the `error` stream of the console by default, maybe not the best possible behavior, but good for starters. If you have a better idea for this, please change it accordingly. --- snippets/httpGet.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index a24dafac5..cc8e6c1d7 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -1,14 +1,18 @@ ### httpGet -Makes a `GET` request to the passed `URL`. +Makes a `GET` request to the passed URL. -Use `XMLHttpRequest` web api to retrieve data from the server. When the response is ready call the callback function. +Use `XMLHttpRequest` web api to retrieve data from the given `url`. +Handle the `onload` event, by running the provided `callback` function. +Handle the `onerror` event, by running the provided `err` function. +Omit the third argument, `err` to log the request to the console's error stream by default. ```js -const httpGet = (url, callback) => { +const httpGet = (url, callback, err = console.error) => { const request = new XMLHttpRequest(); request.open("GET", url, true); request.onload = () => callback(request); + request.onerror = () => err(request); request.send(); } ```