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

156
build/app.bundle.js Normal file
View File

@@ -0,0 +1,156 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Mortgage = function () {
function Mortgage(principal, years, rate) {
_classCallCheck(this, Mortgage);
this.principal = principal;
this.years = years;
this.rate = rate;
}
_createClass(Mortgage, [{
key: "monthlyPayment",
get: function get() {
var monthlyRate = this.rate / 100 / 12;
return this.principal * monthlyRate / (1 - Math.pow(1 / (1 + monthlyRate), this.years * 12));
}
}, {
key: "amortization",
get: function get() {
var monthlyPayment = this.monthlyPayment;
var monthlyRate = this.rate / 100 / 12;
var balance = this.principal;
var amortization = [];
for (var y = 0; y < this.years; y++) {
var interestY = 0;
var principalY = 0;
for (var m = 0; m < 12; m++) {
var interestM = balance * monthlyRate;
var principalM = monthlyPayment - interestM;
interestY = interestY + interestM;
principalY = principalY + principalM;
balance = balance - principalM;
}
amortization.push({ principalY: principalY, interestY: interestY, balance: balance });
}
return amortization;
}
}]);
return Mortgage;
}();
exports.default = Mortgage;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var _mortgage = __webpack_require__(0);
var _mortgage2 = _interopRequireDefault(_mortgage);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
document.getElementById('calcBtn').addEventListener('click', function () {
var principal = document.getElementById("principal").value;
var years = document.getElementById("years").value;
var rate = document.getElementById("rate").value;
var mortgage = new _mortgage2.default(principal, years, rate);
document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2);
document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2);
var html = "";
mortgage.amortization.forEach(function (year, index) {
return html += '\n <tr>\n <td>' + (index + 1) + '</td>\n <td class="currency">' + Math.round(year.principalY) + '</td>\n <td class="stretch">\n <div class="flex">\n <div class="bar principal"\n style="flex:' + year.principalY + ';-webkit-flex:' + year.principalY + '">\n </div>\n <div class="bar interest"\n style="flex:' + year.interestY + ';-webkit-flex:' + year.interestY + '">\n </div>\n </div>\n </td>\n <td class="currency left">' + Math.round(year.interestY) + '</td>\n <td class="currency">' + Math.round(year.balance) + '</td>\n </tr>\n ';
});
document.getElementById("amortization").innerHTML = html;
});
/***/ })
/******/ ]);
//# sourceMappingURL=app.bundle.js.map

1
build/app.bundle.js.map Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,94 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ({
/***/ 2:
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var url = "rates.json";
fetch(url).then(function (response) {
return response.json();
}).then(function (rates) {
var html = '';
rates.forEach(function (rate) {
return html += "<tr><td>" + rate.name + "</td><td>" + rate.years + "</td><td>" + rate.rate + "%</td></tr>";
});
document.getElementById("rates").innerHTML = html;
}).catch(function (e) {
return console.log(e);
});
/***/ })
/******/ });
//# sourceMappingURL=ratefinder.bundle.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["webpack:///webpack/bootstrap 71264ee56fb5fabb88c9?9f3c","webpack:///./js/ratefinder.js"],"names":["url","fetch","then","response","json","html","rates","forEach","rate","name","years","document","getElementById","innerHTML","catch","console","log","e"],"mappings":";AAAA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA,mDAA2C,cAAc;;AAEzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAK;AACL;AACA;;AAEA;AACA;AACA;AACA,mCAA2B,0BAA0B,EAAE;AACvD,yCAAiC,eAAe;AAChD;AACA;AACA;;AAEA;AACA,8DAAsD,+DAA+D;;AAErH;AACA;;AAEA;AACA;;;;;;;;;;;AChEA,IAAIA,MAAM,YAAV;;AAEAC,MAAMD,GAAN,EACKE,IADL,CACU;AAAA,SAAYC,SAASC,IAAT,EAAZ;AAAA,CADV,EAEKF,IAFL,CAEU,iBAAS;AACb,MAAIG,OAAO,EAAX;AACAC,QAAMC,OAAN,CAAc;AAAA,WAAQF,qBAAmBG,KAAKC,IAAxB,iBAAwCD,KAAKE,KAA7C,iBAA8DF,KAAKA,IAAnE,gBAAR;AAAA,GAAd;AACAG,WAASC,cAAT,CAAwB,OAAxB,EAAiCC,SAAjC,GAA6CR,IAA7C;AACD,CANL,EAOKS,KAPL,CAOW;AAAA,SAAKC,QAAQC,GAAR,CAAYC,CAAZ,CAAL;AAAA,CAPX,E","file":"ratefinder.bundle.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// identity function for calling harmony imports with the correct context\n \t__webpack_require__.i = function(value) { return value; };\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 2);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap 71264ee56fb5fabb88c9","let url = \"rates.json\";\r\n\r\nfetch(url)\r\n .then(response => response.json())\r\n .then(rates => {\r\n let html = '';\r\n rates.forEach(rate => html += `<tr><td>${rate.name}</td><td>${rate.years}</td><td>${rate.rate}%</td></tr>`);\r\n document.getElementById(\"rates\").innerHTML = html;\r\n })\r\n .catch(e => console.log(e));\r\n\n\n\n// WEBPACK FOOTER //\n// ./js/ratefinder.js"],"sourceRoot":""}

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));

13
ratefinder.html Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>RateFinder</title>
</head>
<body>
<table id="rates"></table>
<script src="build/ratefinder.bundle.js"></script>
</body>
</html>

View File

@@ -2,10 +2,13 @@ var path = require('path');
var webpack = require('webpack'); var webpack = require('webpack');
module.exports = { module.exports = {
entry: './js/main.js', entry: {
app: './js/main.js',
ratefinder: './js/ratefinder.js'
},
output: { output: {
path: path.resolve(__dirname, 'build'), path: path.resolve(__dirname, 'build'),
filename: 'main.bundle.js' filename: '[name].bundle.js'
}, },
module: { module: {
loaders: [ loaders: [