Skip to content

Commit

Permalink
Update app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gabbyasuncion committed Oct 25, 2020
1 parent 1eaee6e commit 28b0ddb
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
var corsApiUrl = 'https://cors-anywhere.herokuapp.com/';
var corsApiUrl = "https://cors-anywhere.herokuapp.com/";
// TODO: REPLACE YOUR TOKEN
var apiToken = '?token=ZAWafueiwitra0tTaAR2QzY3054rfHOgvv47pBG8Dug';
var apiToken = "?token=YOUR_TOKEN_HERE";

// fetches the data from the API endpoint
// 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;
}
};

// creates the promise
const corsPromise = () => new Promise((resolve, reject) => {
const request = doCORSRequest({ url: "https://trefle.io/api/v1/plants" + apiToken });
resolve(request);
})

//// TODO: ADD WHATEVER FUN CONTENT YOU WANT ////
// 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
});
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 ////

0 comments on commit 28b0ddb

Please sign in to comment.