public/subjects/get-json/README.md

27 lines
963 B
Markdown
Raw Permalink Normal View History

2020-06-10 19:18:53 +00:00
## Get Json
### Instructions
In this exercise, we will focus on building complex async flows with promises.
2022-06-22 04:52:16 +00:00
Create a function named `getJSON` with two parameters:
- `path`: a URL called by your function.
- `params`: optional query parameters that will be appended to the `path`.
2021-04-27 19:04:54 +00:00
2022-06-22 04:52:16 +00:00
`getJSON` must construct a valid url with the `path` and stringified `params`, and use `fetch` to fulfil the request.
2020-06-10 19:18:53 +00:00
2022-06-22 04:52:16 +00:00
If the response is not OK, `getJSON` must throw an error using the response _status text_.
2020-06-10 19:18:53 +00:00
2022-06-22 04:52:16 +00:00
The response body must then be read and parsed from JSON.
2020-06-10 19:18:53 +00:00
The parsed object contains one of those 2 properties:
2022-06-22 04:52:16 +00:00
- `"data"`: the actual data to return.
- `"error"`: the error message to throw.
2020-06-10 19:18:53 +00:00
### Notions
2022-06-22 04:52:16 +00:00
- [Promise.js](https://nan-academy.github.io/js-training/examples/promise.js)
- [Using fetch](https://devdocs.io/dom/fetch_api/using_fetch)
- [URL search params](https://devdocs.io/dom/urlsearchparams)
- [JSON](https://devdocs.io/javascript/global_objects/json)