-
Notifications
You must be signed in to change notification settings - Fork 13
Fetch some data in javascript code
Billy Charlton edited this page Jun 30, 2017
·
3 revisions
« Back to Recipes for typical tasks
Modern javascript has a nice function fetch
which uses promises to grab the contents of a url in the background, and then continue when the background process is complete.
Every function which supports promise
always has a .then()
function, which itself is a promise, so can call its own .then()
function, and so on.
Here, we fetch the CMP auto speeds for a specific year, then parse the JSON response, and then run a function on the parsed data:
function colorByLOS(personJson, year) {
const url = api_server + 'cmp_auto_speeds?year=eq.' + year;
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
for (let segment in data) {
// ... do stuff here
}
})
.catch(function(error) {
console.log(error);
});
}
Note that after the last .then
you need a .catch()
to capture any errors.
Get Started
- Back-End Setup
- Setting up your development environment
- Building Prospector locally
- Publishing your changes
Other Useful Links
- Recipes for typical tasks
- Glossary
- Publishing instructions for CMP standalone site
- Deploying a new release GitHub Pages and Prospector
Platform Considerations
Background Information