Understanding promises

This commit is contained in:
2017-02-14 00:32:39 -08:00
parent 74146a17ee
commit aa4d767204
8 changed files with 300 additions and 2 deletions

20
js/rate-service-mock.js Normal file
View File

@@ -0,0 +1,20 @@
let rates = [
{
"name": "30 years fixed",
"rate": "13",
"years": "30"
},
{
"name": "20 years fixed",
"rate": "2.8",
"years": "20"
}
];
export let findAll = () => new Promise((resolve, reject) => {
if (rates) {
resolve(rates);
} else {
reject("No rates");
}
});

10
js/ratefinder.js Normal file
View File

@@ -0,0 +1,10 @@
let url = "rates.json";
fetch(url)
.then(response => response.json())
.then(rates => {
let html = '';
rates.forEach(rate => html += `<tr><td>${rate.name}</td><td>${rate.years}</td><td>${rate.rate}%</td></tr>`);
document.getElementById("rates").innerHTML = html;
})
.catch(e => console.log(e));