mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
feat: modernize development toolchain
- Add comprehensive development tooling: * ESLint configuration with modern JavaScript rules * Prettier for consistent code formatting * Husky for Git hooks automation * lint-staged for pre-commit checks * Conventional Commits with commitlint * Stylelint for CSS code quality - Fix CSS issues: * Resolve duplicate selectors in layout_note.css * Merge duplicate selectors in layout_list.css * Ensure stylelint compliance - Apply consistent code formatting: * Format all JavaScript, CSS, and Markdown files * Standardize code style across the project * Fix console statement warnings in main.js - Update project documentation: * Enhance README with modern development practices * Update package.json with new scripts and dependencies This modernization establishes a robust development environment with automated code quality checks, consistent formatting~
This commit is contained in:
1 parent
a2a26f618c
commit
172342c8a3
78 files changed
+14986
-3626
No files matched your search
+139
-135
@@ -27,13 +27,13 @@ JavaScript is a lightweight, interpreted programming language.
|
||||
|
||||
```javascript
|
||||
// => Hello world!
|
||||
console.log("Hello world!");
|
||||
console.log('Hello world!');
|
||||
|
||||
// => Hello CheatSheets.zip
|
||||
console.warn("hello %s", "CheatSheets.zip");
|
||||
console.warn('hello %s', 'CheatSheets.zip');
|
||||
|
||||
// Prints error message to stderr
|
||||
console.error(new Error("Oops!"));
|
||||
console.error(new Error('Oops!'));
|
||||
```
|
||||
|
||||
### Numbers
|
||||
@@ -47,7 +47,7 @@ let price = 4.99;
|
||||
|
||||
```javascript
|
||||
let x = null;
|
||||
let name = "Tammy";
|
||||
let name = 'Tammy';
|
||||
const found = false;
|
||||
|
||||
// => Tammy, false, null
|
||||
@@ -60,8 +60,8 @@ console.log(a); // => undefined
|
||||
### Strings
|
||||
|
||||
```javascript
|
||||
let single = "Wheres my bandit hat?";
|
||||
let double = "Wheres my bandit hat?";
|
||||
let single = 'Wheres my bandit hat?';
|
||||
let double = 'Wheres my bandit hat?';
|
||||
|
||||
// => 21
|
||||
console.log(single.length);
|
||||
@@ -107,7 +107,7 @@ console.log(number);
|
||||
let age = 7;
|
||||
|
||||
// String concatenation
|
||||
"Tommy is " + age + " years old.";
|
||||
'Tommy is ' + age + ' years old.';
|
||||
|
||||
// String interpolation
|
||||
`Tommy is ${age} years old.`;
|
||||
@@ -139,7 +139,7 @@ numberOfColumns = 8;
|
||||
const isMailSent = true;
|
||||
|
||||
if (isMailSent) {
|
||||
console.log("Mail sent to recipient");
|
||||
console.log('Mail sent to recipient');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -178,7 +178,7 @@ true && false; // false
|
||||
250 >= 250; // true
|
||||
1 === 1; // true
|
||||
1 === 2; // false
|
||||
1 === "1"; // false
|
||||
1 === '1'; // false
|
||||
```
|
||||
|
||||
#### Logical Operator !
|
||||
@@ -194,12 +194,12 @@ console.log(oppositeValue);
|
||||
#### Nullish coalescing operator ??
|
||||
|
||||
```javascript
|
||||
null ?? "I win"; // 'I win'
|
||||
undefined ?? "Me too"; // 'Me too'
|
||||
null ?? 'I win'; // 'I win'
|
||||
undefined ?? 'Me too'; // 'Me too'
|
||||
|
||||
false ?? "I lose"; // false
|
||||
0 ?? "I lose again"; // 0
|
||||
"" ?? "Damn it"; // ''
|
||||
false ?? 'I lose'; // false
|
||||
0 ?? 'I lose again'; // 0
|
||||
'' ?? 'Damn it'; // ''
|
||||
```
|
||||
|
||||
### else if
|
||||
@@ -208,13 +208,13 @@ false ?? "I lose"; // false
|
||||
const size = 10;
|
||||
|
||||
if (size > 100) {
|
||||
console.log("Big");
|
||||
console.log('Big');
|
||||
} else if (size > 20) {
|
||||
console.log("Medium");
|
||||
console.log('Medium');
|
||||
} else if (size > 4) {
|
||||
console.log("Small");
|
||||
console.log('Small');
|
||||
} else {
|
||||
console.log("Tiny");
|
||||
console.log('Tiny');
|
||||
}
|
||||
// Print: Small
|
||||
```
|
||||
@@ -222,17 +222,17 @@ if (size > 100) {
|
||||
### switch Statement
|
||||
|
||||
```javascript
|
||||
const food = "salad";
|
||||
const food = 'salad';
|
||||
|
||||
switch (food) {
|
||||
case "oyster":
|
||||
console.log("The taste of the sea");
|
||||
case 'oyster':
|
||||
console.log('The taste of the sea');
|
||||
break;
|
||||
case "pizza":
|
||||
console.log("A delicious pie");
|
||||
case 'pizza':
|
||||
console.log('A delicious pie');
|
||||
break;
|
||||
default:
|
||||
console.log("Enjoy your meal");
|
||||
console.log('Enjoy your meal');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -241,12 +241,12 @@ switch (food) {
|
||||
```javascript
|
||||
0 == false; // true
|
||||
0 === false; // false, different type
|
||||
1 == "1"; // true, automatic type conversion
|
||||
1 === "1"; // false, different type
|
||||
1 == '1'; // true, automatic type conversion
|
||||
1 === '1'; // false, different type
|
||||
null == undefined; // true
|
||||
null === undefined; // false
|
||||
"0" == false; // true
|
||||
"0" === false; // false
|
||||
'0' == false; // true
|
||||
'0' === false; // false
|
||||
```
|
||||
|
||||
The `==` just check the value, `===` check both the value and the type.
|
||||
@@ -270,12 +270,12 @@ sum(3, 6); // 9
|
||||
```javascript
|
||||
// Named function
|
||||
function rocketToMars() {
|
||||
return "BOOM!";
|
||||
return 'BOOM!';
|
||||
}
|
||||
|
||||
// Anonymous function
|
||||
const rocketToMars = function () {
|
||||
return "BOOM!";
|
||||
return 'BOOM!';
|
||||
};
|
||||
```
|
||||
|
||||
@@ -294,7 +294,7 @@ console.log(sum(2, 5)); // => 7
|
||||
|
||||
```javascript
|
||||
const printHello = () => {
|
||||
console.log("hello");
|
||||
console.log('hello');
|
||||
};
|
||||
printHello(); // => hello
|
||||
```
|
||||
@@ -349,7 +349,7 @@ sum(2, 4); // 6
|
||||
|
||||
```javascript
|
||||
const dog = function () {
|
||||
return "Woof!";
|
||||
return 'Woof!';
|
||||
};
|
||||
```
|
||||
|
||||
@@ -376,7 +376,7 @@ function add(num1, num2) {
|
||||
|
||||
```javascript
|
||||
function myFunction() {
|
||||
var pizzaName = "Margarita";
|
||||
var pizzaName = 'Margarita';
|
||||
// Code here can use pizzaName
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ function myFunction() {
|
||||
const isLoggedIn = true;
|
||||
|
||||
if (isLoggedIn == true) {
|
||||
const statusMessage = "Logged in.";
|
||||
const statusMessage = 'Logged in.';
|
||||
}
|
||||
|
||||
// Uncaught ReferenceError...
|
||||
@@ -400,7 +400,7 @@ console.log(statusMessage);
|
||||
|
||||
```javascript
|
||||
// Variable declared globally
|
||||
const color = "blue";
|
||||
const color = 'blue';
|
||||
|
||||
function printColor() {
|
||||
console.log(color);
|
||||
@@ -455,10 +455,10 @@ The variable has its own copy using `let`, and the variable has shared copy usin
|
||||
### Arrays
|
||||
|
||||
```javascript
|
||||
const fruits = ["apple", "orange", "banana"];
|
||||
const fruits = ['apple', 'orange', 'banana'];
|
||||
|
||||
// Different data types
|
||||
const data = [1, "chicken", false];
|
||||
const data = [1, 'chicken', false];
|
||||
```
|
||||
|
||||
### Property .length
|
||||
@@ -494,8 +494,8 @@ console.log(myArray[1]); // 200
|
||||
|
||||
```javascript
|
||||
// Adding a single element:
|
||||
const cart = ["apple", "orange"];
|
||||
cart.push("pear");
|
||||
const cart = ['apple', 'orange'];
|
||||
cart.push('pear');
|
||||
|
||||
// Adding multiple elements:
|
||||
const numbers = [1, 2];
|
||||
@@ -507,7 +507,7 @@ Add items to the end and returns the new array length.
|
||||
### Array.pop()
|
||||
|
||||
```javascript
|
||||
const fruits = ["apple", "orange", "banana"];
|
||||
const fruits = ['apple', 'orange', 'banana'];
|
||||
|
||||
const fruit = fruits.pop(); // 'banana'
|
||||
console.log(fruits); // ["apple", "orange"]
|
||||
@@ -518,7 +518,7 @@ Remove an item from the end and returns the removed item.
|
||||
### Array.shift()
|
||||
|
||||
```javascript
|
||||
let cats = ["Bob", "Willy", "Mini"];
|
||||
let cats = ['Bob', 'Willy', 'Mini'];
|
||||
|
||||
cats.shift(); // ['Willy', 'Mini']
|
||||
```
|
||||
@@ -528,13 +528,13 @@ Remove an item from the beginning and returns the removed item.
|
||||
### Array.unshift()
|
||||
|
||||
```javascript
|
||||
let cats = ["Bob"];
|
||||
let cats = ['Bob'];
|
||||
|
||||
// => ['Willy', 'Bob']
|
||||
cats.unshift("Willy");
|
||||
cats.unshift('Willy');
|
||||
|
||||
// => ['Puff', 'George', 'Willy', 'Bob']
|
||||
cats.unshift("Puff", "George");
|
||||
cats.unshift('Puff', 'George');
|
||||
```
|
||||
|
||||
Add items to the beginning and returns the new array length.
|
||||
@@ -563,7 +563,7 @@ If you want to avoid mutating your original array, you can use concat.
|
||||
const emptySet = new Set();
|
||||
|
||||
// Set Object with values
|
||||
const setObj = new Set([1, true, "hi"]);
|
||||
const setObj = new Set([1, true, 'hi']);
|
||||
```
|
||||
|
||||
### Add
|
||||
@@ -572,19 +572,19 @@ const setObj = new Set([1, true, "hi"]);
|
||||
const emptySet = new Set();
|
||||
|
||||
// add values
|
||||
emptySet.add("a"); // 'a'
|
||||
emptySet.add('a'); // 'a'
|
||||
emptySet.add(1); // 'a', 1
|
||||
emptySet.add(true); // 'a', 1, true
|
||||
emptySet.add("a"); // 'a', 1, true
|
||||
emptySet.add('a'); // 'a', 1, true
|
||||
```
|
||||
|
||||
### Delete
|
||||
|
||||
```javascript
|
||||
const emptySet = new Set([1, true, "a"]);
|
||||
const emptySet = new Set([1, true, 'a']);
|
||||
|
||||
// delete values
|
||||
emptySet.delete("a"); // 1, true
|
||||
emptySet.delete('a'); // 1, true
|
||||
emptySet.delete(true); // 1
|
||||
emptySet.delete(1); //
|
||||
```
|
||||
@@ -592,10 +592,10 @@ emptySet.delete(1); //
|
||||
### Has
|
||||
|
||||
```javascript
|
||||
const setObj = new Set([1, true, "a"]);
|
||||
const setObj = new Set([1, true, 'a']);
|
||||
|
||||
// returns true or false
|
||||
setObj.has("a"); // true
|
||||
setObj.has('a'); // true
|
||||
setObj.has(1); // true
|
||||
setObj.has(false); // false
|
||||
```
|
||||
@@ -603,7 +603,7 @@ setObj.has(false); // false
|
||||
### Clear
|
||||
|
||||
```javascript
|
||||
const setObj = new Set([1, true, "a"]);
|
||||
const setObj = new Set([1, true, 'a']);
|
||||
|
||||
// clears the set
|
||||
console.log(setObj); // 1, true, 'a'
|
||||
@@ -613,7 +613,7 @@ setObj.clear(); //
|
||||
### Size
|
||||
|
||||
```javascript
|
||||
const setObj = new Set([1, true, "a"]);
|
||||
const setObj = new Set([1, true, 'a']);
|
||||
|
||||
consoloe.log(setObj.size); // 3
|
||||
```
|
||||
@@ -621,7 +621,7 @@ consoloe.log(setObj.size); // 3
|
||||
### ForEach
|
||||
|
||||
```javascript
|
||||
const setObj = new Set([1, true, "a"]);
|
||||
const setObj = new Set([1, true, 'a']);
|
||||
|
||||
setObj.forEach(function (value) {
|
||||
console.log(value);
|
||||
@@ -651,7 +651,7 @@ while (i < 5) {
|
||||
### Reverse Loop
|
||||
|
||||
```javascript
|
||||
const fruits = ["apple", "orange", "banana"];
|
||||
const fruits = ['apple', 'orange', 'banana'];
|
||||
|
||||
for (let i = fruits.length - 1; i >= 0; i--) {
|
||||
console.log(`${i}. ${fruits[i]}`);
|
||||
@@ -715,7 +715,7 @@ for (i = 0; i < 10; i++) {
|
||||
if (i === 3) {
|
||||
continue;
|
||||
}
|
||||
text += "The number is " + i + "<br>";
|
||||
text += 'The number is ' + i + '<br>';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -732,7 +732,7 @@ for (let i = 0; i < 2; i += 1) {
|
||||
### for...in loop
|
||||
|
||||
```javascript
|
||||
const fruits = ["apple", "orange", "banana"];
|
||||
const fruits = ['apple', 'orange', 'banana'];
|
||||
|
||||
for (let index in fruits) {
|
||||
console.log(index);
|
||||
@@ -745,7 +745,7 @@ for (let index in fruits) {
|
||||
### for...of loop
|
||||
|
||||
```javascript
|
||||
const fruits = ["apple", "orange", "banana"];
|
||||
const fruits = ['apple', 'orange', 'banana'];
|
||||
|
||||
for (let fruit of fruits) {
|
||||
console.log(fruit);
|
||||
@@ -803,10 +803,10 @@ console.log(sum); // 10
|
||||
### Array.map()
|
||||
|
||||
```javascript
|
||||
const members = ["Taylor", "Donald", "Don", "Natasha", "Bobby"];
|
||||
const members = ['Taylor', 'Donald', 'Don', 'Natasha', 'Bobby'];
|
||||
|
||||
const announcements = members.map((member) => {
|
||||
return member + " joined the contest.";
|
||||
return member + ' joined the contest.';
|
||||
});
|
||||
|
||||
console.log(announcements);
|
||||
@@ -837,8 +837,8 @@ const filteredArray = randomNumbers.filter((n) => {
|
||||
|
||||
```javascript
|
||||
const apple = {
|
||||
color: "Green",
|
||||
price: { bulk: "$3/kg", smallQty: "$4/kg" },
|
||||
color: 'Green',
|
||||
price: { bulk: '$3/kg', smallQty: '$4/kg' }
|
||||
};
|
||||
console.log(apple.color); // => Green
|
||||
console.log(apple.price.bulk); // => $3/kg
|
||||
@@ -862,7 +862,7 @@ const trainSchedule = {
|
||||
|
||||
```javascript
|
||||
const classElection = {
|
||||
date: "January 12",
|
||||
date: 'January 12'
|
||||
};
|
||||
|
||||
console.log(classElection.place); // undefined
|
||||
@@ -872,16 +872,16 @@ console.log(classElection.place); // undefined
|
||||
|
||||
```javascript
|
||||
const student = {
|
||||
name: "Sheldon",
|
||||
name: 'Sheldon',
|
||||
score: 100,
|
||||
grade: "A",
|
||||
grade: 'A'
|
||||
};
|
||||
|
||||
console.log(student);
|
||||
// { name: 'Sheldon', score: 100, grade: 'A' }
|
||||
|
||||
delete student.score;
|
||||
student.grade = "F";
|
||||
student.grade = 'F';
|
||||
console.log(student);
|
||||
// { name: 'Sheldon', grade: 'F' }
|
||||
|
||||
@@ -893,8 +893,8 @@ student = {};
|
||||
|
||||
```javascript
|
||||
const person = {
|
||||
name: "Tom",
|
||||
age: "22",
|
||||
name: 'Tom',
|
||||
age: '22'
|
||||
};
|
||||
const { name, age } = person;
|
||||
console.log(name); // 'Tom'
|
||||
@@ -905,10 +905,10 @@ console.log(age); // '22'
|
||||
|
||||
```javascript
|
||||
const person = {
|
||||
firstName: "Matilda",
|
||||
firstName: 'Matilda',
|
||||
age: 27,
|
||||
hobby: "knitting",
|
||||
goal: "learning JavaScript",
|
||||
hobby: 'knitting',
|
||||
goal: 'learning JavaScript'
|
||||
};
|
||||
|
||||
delete person.hobby; // or delete person[hobby];
|
||||
@@ -927,11 +927,11 @@ console.log(person);
|
||||
|
||||
```javascript
|
||||
const origNum = 8;
|
||||
const origObj = { color: "blue" };
|
||||
const origObj = { color: 'blue' };
|
||||
|
||||
const changeItUp = (num, obj) => {
|
||||
num = 7;
|
||||
obj.color = "red";
|
||||
obj.color = 'red';
|
||||
};
|
||||
|
||||
changeItUp(origNum, origObj);
|
||||
@@ -947,7 +947,7 @@ console.log(origObj.color);
|
||||
### Shorthand object creation
|
||||
|
||||
```javascript
|
||||
const activity = "Surfing";
|
||||
const activity = 'Surfing';
|
||||
const beach = { activity };
|
||||
console.log(beach); // { activity: 'Surfing' }
|
||||
```
|
||||
@@ -956,11 +956,11 @@ console.log(beach); // { activity: 'Surfing' }
|
||||
|
||||
```javascript
|
||||
const cat = {
|
||||
name: "Pipey",
|
||||
name: 'Pipey',
|
||||
age: 8,
|
||||
whatName() {
|
||||
return this.name;
|
||||
},
|
||||
}
|
||||
};
|
||||
console.log(cat.whatName()); // => Pipey
|
||||
```
|
||||
@@ -977,8 +977,8 @@ const dogFactory = (name, age, breed) => {
|
||||
age: age,
|
||||
breed: breed,
|
||||
bark() {
|
||||
console.log("Woof!");
|
||||
},
|
||||
console.log('Woof!');
|
||||
}
|
||||
};
|
||||
};
|
||||
```
|
||||
@@ -993,11 +993,11 @@ const engine = {
|
||||
},
|
||||
// anonymous arrow function expression with no arguments
|
||||
sputter: () => {
|
||||
console.log("The engine sputters...");
|
||||
},
|
||||
console.log('The engine sputters...');
|
||||
}
|
||||
};
|
||||
|
||||
engine.start("noisily");
|
||||
engine.start('noisily');
|
||||
engine.sputter();
|
||||
```
|
||||
|
||||
@@ -1005,20 +1005,20 @@ engine.sputter();
|
||||
|
||||
```javascript
|
||||
const myCat = {
|
||||
_name: "Dottie",
|
||||
_name: 'Dottie',
|
||||
get name() {
|
||||
return this._name;
|
||||
},
|
||||
set name(newName) {
|
||||
this._name = newName;
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
// Reference invokes the getter
|
||||
console.log(myCat.name);
|
||||
|
||||
// Assignment invokes the setter
|
||||
myCat.name = "Yankee";
|
||||
myCat.name = 'Yankee';
|
||||
```
|
||||
|
||||
## JavaScript Classes
|
||||
@@ -1032,16 +1032,16 @@ class Dog {
|
||||
}
|
||||
|
||||
introduce() {
|
||||
console.log("This is " + this._name + " !");
|
||||
console.log('This is ' + this._name + ' !');
|
||||
}
|
||||
|
||||
// A static method
|
||||
static bark() {
|
||||
console.log("Woof!");
|
||||
console.log('Woof!');
|
||||
}
|
||||
}
|
||||
|
||||
const myDog = new Dog("Buster");
|
||||
const myDog = new Dog('Buster');
|
||||
myDog.introduce();
|
||||
|
||||
// Calling the static method
|
||||
@@ -1058,7 +1058,7 @@ class Song {
|
||||
}
|
||||
|
||||
play() {
|
||||
console.log("Song playing!");
|
||||
console.log('Song playing!');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1076,7 +1076,7 @@ class Song {
|
||||
}
|
||||
}
|
||||
|
||||
const mySong = new Song("Bohemian Rhapsody", "Queen");
|
||||
const mySong = new Song('Bohemian Rhapsody', 'Queen');
|
||||
console.log(mySong.title);
|
||||
```
|
||||
|
||||
@@ -1085,11 +1085,11 @@ console.log(mySong.title);
|
||||
```javascript
|
||||
class Song {
|
||||
play() {
|
||||
console.log("Playing!");
|
||||
console.log('Playing!');
|
||||
}
|
||||
|
||||
stop() {
|
||||
console.log("Stopping!");
|
||||
console.log('Stopping!');
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -1114,9 +1114,9 @@ class Song extends Media {
|
||||
}
|
||||
|
||||
const mySong = new Song({
|
||||
artist: "Queen",
|
||||
name: "Bohemian Rhapsody",
|
||||
publishDate: 1975,
|
||||
artist: 'Queen',
|
||||
name: 'Bohemian Rhapsody',
|
||||
publishDate: 1975
|
||||
});
|
||||
```
|
||||
|
||||
@@ -1185,7 +1185,7 @@ module.exports = {
|
||||
add,
|
||||
subtract,
|
||||
multiply,
|
||||
duplicate,
|
||||
duplicate
|
||||
};
|
||||
```
|
||||
|
||||
@@ -1193,7 +1193,7 @@ module.exports = {
|
||||
|
||||
```javascript
|
||||
// main.js
|
||||
const myMath = require("./myMath.js");
|
||||
const myMath = require('./myMath.js');
|
||||
|
||||
console.log(myMath.add(6, 2)); // 8
|
||||
console.log(myMath.subtract(6, 2)); // 4
|
||||
@@ -1210,15 +1210,15 @@ const promise = new Promise((resolve, reject) => {
|
||||
const res = true;
|
||||
// An asynchronous operation.
|
||||
if (res) {
|
||||
resolve("Resolved!");
|
||||
resolve('Resolved!');
|
||||
} else {
|
||||
reject(Error("Error"));
|
||||
reject(Error('Error'));
|
||||
}
|
||||
});
|
||||
|
||||
promise.then(
|
||||
(res) => console.log(res),
|
||||
(err) => console.error(err),
|
||||
(err) => console.error(err)
|
||||
);
|
||||
```
|
||||
|
||||
@@ -1226,7 +1226,7 @@ promise.then(
|
||||
|
||||
```javascript
|
||||
const executorFn = (resolve, reject) => {
|
||||
resolve("Resolved!");
|
||||
resolve('Resolved!');
|
||||
};
|
||||
|
||||
const promise = new Promise(executorFn);
|
||||
@@ -1236,7 +1236,7 @@ const promise = new Promise(executorFn);
|
||||
|
||||
```javascript
|
||||
const loginAlert = () => {
|
||||
console.log("Login");
|
||||
console.log('Login');
|
||||
};
|
||||
|
||||
setTimeout(loginAlert, 6000);
|
||||
@@ -1247,7 +1247,7 @@ setTimeout(loginAlert, 6000);
|
||||
```javascript
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve("Result");
|
||||
resolve('Result');
|
||||
}, 200);
|
||||
});
|
||||
|
||||
@@ -1257,7 +1257,7 @@ promise.then(
|
||||
},
|
||||
(err) => {
|
||||
console.error(err);
|
||||
},
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
@@ -1266,7 +1266,7 @@ promise.then(
|
||||
```javascript
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(Error("Promise Rejected Unconditionally."));
|
||||
reject(Error('Promise Rejected Unconditionally.'));
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
@@ -1320,7 +1320,7 @@ Promise.allSettled([promise1, promise2]).then((res) => {
|
||||
```javascript
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve("*");
|
||||
resolve('*');
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
@@ -1329,7 +1329,7 @@ const twoStars = (star) => {
|
||||
};
|
||||
|
||||
const oneDot = (star) => {
|
||||
return star + ".";
|
||||
return star + '.';
|
||||
};
|
||||
|
||||
const print = (val) => {
|
||||
@@ -1344,7 +1344,7 @@ promise.then(twoStars).then(oneDot).then(print);
|
||||
|
||||
```javascript
|
||||
const executorFn = (resolve, reject) => {
|
||||
console.log("The executor function of the promise!");
|
||||
console.log('The executor function of the promise!');
|
||||
};
|
||||
|
||||
const promise = new Promise(executorFn);
|
||||
@@ -1353,11 +1353,15 @@ const promise = new Promise(executorFn);
|
||||
### Chaining multiple .then()
|
||||
|
||||
```javascript
|
||||
const promise = new Promise((resolve) => setTimeout(() => resolve("dAlan"), 100));
|
||||
const promise = new Promise((resolve) =>
|
||||
setTimeout(() => resolve('dAlan'), 100)
|
||||
);
|
||||
|
||||
promise
|
||||
.then((res) => {
|
||||
return res === "Alan" ? Promise.resolve("Hey Alan!") : Promise.reject("Who are you?");
|
||||
return res === 'Alan'
|
||||
? Promise.resolve('Hey Alan!')
|
||||
: Promise.reject('Who are you?');
|
||||
})
|
||||
.then(
|
||||
(res) => {
|
||||
@@ -1365,7 +1369,7 @@ promise
|
||||
},
|
||||
(err) => {
|
||||
console.error(err);
|
||||
},
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
@@ -1378,7 +1382,7 @@ const mock = (success, timeout = 1000) => {
|
||||
if (success) {
|
||||
resolve({ status: 200, data: {} });
|
||||
} else {
|
||||
reject({ message: "Error" });
|
||||
reject({ message: 'Error' });
|
||||
}
|
||||
}, timeout);
|
||||
});
|
||||
@@ -1400,7 +1404,7 @@ const someEvent = async () => {
|
||||
function helloWorld() {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve("Hello World!");
|
||||
resolve('Hello World!');
|
||||
}, 2000);
|
||||
});
|
||||
}
|
||||
@@ -1408,13 +1412,13 @@ function helloWorld() {
|
||||
const msg = async function () {
|
||||
//Async Function Expression
|
||||
const msg = await helloWorld();
|
||||
console.log("Message:", msg);
|
||||
console.log('Message:', msg);
|
||||
};
|
||||
|
||||
const msg1 = async () => {
|
||||
//Async Arrow Function
|
||||
const msg = await helloWorld();
|
||||
console.log("Message:", msg);
|
||||
console.log('Message:', msg);
|
||||
};
|
||||
|
||||
msg(); // Message: Hello World! <-- after 2 seconds
|
||||
@@ -1427,7 +1431,7 @@ msg1(); // Message: Hello World! <-- after 2 seconds
|
||||
let pro1 = Promise.resolve(5);
|
||||
let pro2 = 44;
|
||||
let pro3 = new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, 100, "foo");
|
||||
setTimeout(resolve, 100, 'foo');
|
||||
});
|
||||
|
||||
Promise.all([pro1, pro2, pro3]).then(function (values) {
|
||||
@@ -1442,14 +1446,14 @@ Promise.all([pro1, pro2, pro3]).then(function (values) {
|
||||
function helloWorld() {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve("Hello World!");
|
||||
resolve('Hello World!');
|
||||
}, 2000);
|
||||
});
|
||||
}
|
||||
|
||||
async function msg() {
|
||||
const msg = await helloWorld();
|
||||
console.log("Message:", msg);
|
||||
console.log('Message:', msg);
|
||||
}
|
||||
|
||||
msg(); // Message: Hello World! <-- after 2 seconds
|
||||
@@ -1464,7 +1468,7 @@ try {
|
||||
let user = JSON.parse(json); // <-- no errors
|
||||
console.log(user.name); // no name!
|
||||
} catch (e) {
|
||||
console.error("Invalid JSON data!");
|
||||
console.error('Invalid JSON data!');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1474,14 +1478,14 @@ try {
|
||||
function helloWorld() {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve("Hello World!");
|
||||
resolve('Hello World!');
|
||||
}, 2000);
|
||||
});
|
||||
}
|
||||
|
||||
async function msg() {
|
||||
const msg = await helloWorld();
|
||||
console.log("Message:", msg);
|
||||
console.log('Message:', msg);
|
||||
}
|
||||
|
||||
msg(); // Message: Hello World! <-- after 2 seconds
|
||||
@@ -1505,7 +1509,7 @@ Also see: [JSON cheatsheet](/json)
|
||||
|
||||
```javascript
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "mysite.com/getjson");
|
||||
xhr.open('GET', 'mysite.com/getjson');
|
||||
```
|
||||
|
||||
`XMLHttpRequest` is a browser-level API that enables the client to script data transfers via JavaScript, NOT part of the
|
||||
@@ -1515,8 +1519,8 @@ JavaScript language.
|
||||
|
||||
```javascript
|
||||
const req = new XMLHttpRequest();
|
||||
req.responseType = "json";
|
||||
req.open("GET", "/getdata?id=65");
|
||||
req.responseType = 'json';
|
||||
req.open('GET', '/getdata?id=65');
|
||||
req.onload = () => {
|
||||
console.log(xhr.response);
|
||||
};
|
||||
@@ -1528,13 +1532,13 @@ req.send();
|
||||
|
||||
```javascript
|
||||
const data = {
|
||||
fish: "Salmon",
|
||||
weight: "1.5 KG",
|
||||
units: 5,
|
||||
fish: 'Salmon',
|
||||
weight: '1.5 KG',
|
||||
units: 5
|
||||
};
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/inventory/add");
|
||||
xhr.responseType = "json";
|
||||
xhr.open('POST', '/inventory/add');
|
||||
xhr.responseType = 'json';
|
||||
xhr.send(JSON.stringify(data));
|
||||
|
||||
xhr.onload = () => {
|
||||
@@ -1566,7 +1570,7 @@ fetch(url, {
|
||||
### JSON Formatted
|
||||
|
||||
```javascript
|
||||
fetch("url-that-returns-JSON")
|
||||
fetch('url-that-returns-JSON')
|
||||
.then((response) => response.json())
|
||||
.then((jsonResponse) => {
|
||||
console.log(jsonResponse);
|
||||
@@ -1589,20 +1593,20 @@ fetch('url')
|
||||
### Fetch API Function
|
||||
|
||||
```javascript
|
||||
fetch("https://api-xxx.com/endpoint", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ id: "200" }),
|
||||
fetch('https://api-xxx.com/endpoint', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ id: '200' })
|
||||
})
|
||||
.then(
|
||||
(response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
throw new Error("Request failed!");
|
||||
throw new Error('Request failed!');
|
||||
},
|
||||
(networkError) => {
|
||||
console.log(networkError.message);
|
||||
},
|
||||
}
|
||||
)
|
||||
.then((jsonResponse) => {
|
||||
console.log(jsonResponse);
|
||||
@@ -1616,7 +1620,7 @@ const getSuggestions = async () => {
|
||||
const wordQuery = inputField.value;
|
||||
const endpoint = `${url}${queryParams}${wordQuery}`;
|
||||
try {
|
||||
const response = await fetch(endpoint, { cache: "no-cache" });
|
||||
const response = await fetch(endpoint, { cache: 'no-cache' });
|
||||
if (response.ok) {
|
||||
const jsonResponse = await response.json();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user