Node examples, and minor fixes
This commit is contained in:
+28
-11
@@ -5,11 +5,9 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "flow-remove-types src/ -d out/",
|
||||
"buildWithMaps":
|
||||
"flow-remove-types src/ -d out/ --pretty --sourcemaps",
|
||||
"buildWithMaps": "flow-remove-types src/ -d out/ --pretty --sourcemaps",
|
||||
"start": "npm run build && node out/doroundmath.js",
|
||||
"nodemon":
|
||||
"./node_modules/nodemon/bin/nodemon.js --watch src --delay 1 --exec npm start",
|
||||
"nodemon": "nodemon --watch src --delay 1 --exec npm start",
|
||||
"addTypes": "flow-typed install",
|
||||
"update": "npm install && flow-typed install",
|
||||
"flow": "flow",
|
||||
@@ -20,7 +18,10 @@
|
||||
"author": "Federico Kereki",
|
||||
"license": "ISC",
|
||||
"babel": {
|
||||
"presets": ["env", "flow"]
|
||||
"presets": [
|
||||
"env",
|
||||
"flow"
|
||||
]
|
||||
},
|
||||
"eslintConfig": {
|
||||
"parserOptions": {
|
||||
@@ -33,21 +34,37 @@
|
||||
"node": true,
|
||||
"es6": true
|
||||
},
|
||||
"extends": ["eslint:recommended", "plugin:flowtype/recommended"],
|
||||
"plugins": ["babel", "flowtype"],
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:flowtype/recommended"
|
||||
],
|
||||
"plugins": [
|
||||
"babel",
|
||||
"flowtype"
|
||||
],
|
||||
"rules": {
|
||||
"no-console": "off",
|
||||
"no-var": "error",
|
||||
"prefer-const": "error"
|
||||
}
|
||||
},
|
||||
"eslintIgnore": ["**/out/*.js"],
|
||||
"eslintIgnore": [
|
||||
"**/out/*.js"
|
||||
],
|
||||
"flow-coverage-report": {
|
||||
"concurrentFiles": 1,
|
||||
"excludeGlob": ["node_modules/**"],
|
||||
"includeGlob": ["src/**/*.js"],
|
||||
"excludeGlob": [
|
||||
"node_modules/**"
|
||||
],
|
||||
"includeGlob": [
|
||||
"src/**/*.js"
|
||||
],
|
||||
"threshold": 90,
|
||||
"type": ["text", "html", "json"]
|
||||
"type": [
|
||||
"text",
|
||||
"html",
|
||||
"json"
|
||||
]
|
||||
},
|
||||
"prettier": {
|
||||
"tabWidth": 4,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
const http = require("http");
|
||||
|
||||
http
|
||||
.createServer((req, res) => {
|
||||
res.writeHead(200, { "Content-Type": "text/plain" });
|
||||
res.end("Server alive!");
|
||||
})
|
||||
.listen(8080, "localhost");
|
||||
|
||||
console.log("Mini server ready at http://localhost:8080/");
|
||||
@@ -0,0 +1,49 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const util = require("util");
|
||||
|
||||
const FILE_TO_READ = "/home/fkereki/MODERNJS/chapter03/src/promisify.js"; // its own source!
|
||||
|
||||
// 1. Original error-first style callback
|
||||
|
||||
function showFileLength1(fileName: string): void {
|
||||
fs.readFile(fileName, "utf8", (err, text) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
console.log(`1. Reading, old style: ${text.length} bytes`);
|
||||
}
|
||||
});
|
||||
}
|
||||
showFileLength1(FILE_TO_READ);
|
||||
|
||||
// 2. Alternative style using promises
|
||||
|
||||
function showFileLength2(fileName: string): void {
|
||||
const readFile = util.promisify(fs.readFile);
|
||||
|
||||
readFile(fileName, "utf8")
|
||||
.then((text: string) => {
|
||||
console.log(`2. Reading with promises: ${text.length} bytes`);
|
||||
})
|
||||
.catch((err: mixed) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
showFileLength2(FILE_TO_READ);
|
||||
|
||||
// 3. Using async/await, and with an arrow function just for variety
|
||||
|
||||
const showFileLength3 = async (fileName: string) => {
|
||||
const readFile = util.promisify(fs.readFile);
|
||||
|
||||
try {
|
||||
const text: string = await readFile(fileName, "utf8");
|
||||
console.log(`3. Reading with async/await: ${text.length} bytes`);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
showFileLength3(FILE_TO_READ);
|
||||
@@ -1,13 +1,17 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
// These won't be exported:
|
||||
|
||||
const roundToCents = (x: number): number => Math.round(x * 100) / 100;
|
||||
|
||||
const changeSign = (x: number): number => -x;
|
||||
|
||||
// The following will be exported:
|
||||
|
||||
const addR = (x: number, y: number): number => roundToCents(x + y);
|
||||
|
||||
const subR = (x: number, y: number) => addR(x, changeSign(y));
|
||||
const subR = (x: number, y: number): number => addR(x, changeSign(y));
|
||||
|
||||
const multR = (x: number, y: number): number => roundToCents(x * y);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user