-
Notifications
You must be signed in to change notification settings - Fork 93
/
app.js
31 lines (27 loc) · 1.04 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var corsApiUrl = "https://cors-anywhere.herokuapp.com/";
// TODO: REPLACE YOUR TOKEN
var apiToken = "?token=YOUR_TOKEN_HERE";
// CORS stands for "cross origin resource sharing" -- you'll be making http requests in order
// DON'T CHANGE THIS: fetches the data from the API endpoint
const doCORSRequest = (options) => {
var x = new XMLHttpRequest();
x.open("GET", corsApiUrl + options.url);
x.send(options.data);
return x;
};
// Example promise that executes the GET request above and waits for it to finish before resolving
const corsPromise = () =>
new Promise((resolve, reject) => {
const request = doCORSRequest({
url: "https://trefle.io/api/v1/plants" + apiToken,
});
resolve(request);
});
// THIS IS SOME SAMPLE CODE FOR HOW TO USE PROMISES -- feel free to adapt this into a function!
corsPromise().then(
(request) =>
(request.onload = request.onerror = function () {
// TODO: ADD FUNCTION, ETC. FOR WHATEVER YOU WANT TO DO ONCE THE DATA IS RECEIVED
})
);
//// TODO: ADD WHATEVER FUN CONTENT YOU WANT ////