HNVN TR JavaScript 4-5 Presentation
HNVN TR JavaScript 4-5 Presentation
• Course overview.
• Course contents.
• Examination.
Course Overview
• Introduction to JavaScript.
• Why learn JavaScript?
• The fundamentals of JavaScript.
• JavaScript new features: ES2015 à ES2018.
Course Contents
• Clean format.
• `this` vs Arrow fn.
const arr = [1, 2, 3];
const squares = arr.map(x => x * x);
const obj = {
t: 50,
lazyComp() {
setTimeout(() => console.log(this.t), 1000);
}
}
Arrow Functions
• Specifying parameters:
• Specifying a body:
// eg
const squares = [1, 2, 3]
.map(function (x) { return x * x });
gx(3)
gx(3, 5)
Destructuring Assignment
const obj = {
v: {
g: 5
}
};
const { v: { g } } = obj;
Destructuring Assignment
function add(...args) {
return args.reduce((acc, v) => acc + v);
}
add(2, 3, 4, 5)
//14
Rest Operators
const obj = {
a: 5,
b: 10,
c: 35
};
const obj = {
a: 5,
b: 10,
c: 35
};
const ba = {
...obj,
b: 50
};
Spread Operators
// OK
new Shape();
function Shape() {
}
// Error
new Shape();
class Shape {
}
ES2015 Class
http://exploringjs.com/es6/ch_modules.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
ES2015 Module
import defaultExport, {
export [ , [...] ]
} from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import(module-name);
http://exploringjs.com/es6/ch_modules.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
ES2015 Module
You can’t nest import inside if statements, functions, etc.
if (true) {
import { something } from './src/lib';
}
ES2015 Module
export { name1, name2, …, nameN };
export { variable1 as name1, variable2 as name2, …,
nameN };
export let name1, name2, …, nameN; // also var,
const
export let name1 = …, name2 = …, …, nameN; // also
var, const
export function FunctionName(){...}
export class ClassName {...}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
ES2015 Module
export default expression;
export default function (…) { … } // also class,
function*
export default function name1(…) { … } // also
class, function*
export { name1 as default, … };
export * from …;
export { name1, name2, …, nameN } from …;
export {
import1 as name1, import2 as name2, …, nameN
} from …;
export { default } from …;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
DEMO
Q&A
Section 5
Promise, Async/Await
ES2015 Promise
• Create a promise
• Consuming a Promise
promise
.then(
value => { /* fulfillment */ },
error => { /* rejection */ }
)
.catch(error => { /* rejection */ });
ES2015 Promise
function httpGet(url) {
return new Promise(
function (resolve, reject) {
const request = new XMLHttpRequest();
request.onload = function () {
if (this.status === 200) {
// Success
resolve(this.response);
} else {
// Something went wrong (404 etc.)
reject(new Error(this.statusText));
}
};
request.onerror = function () {
reject(new Error('XMLHttpRequest Error: '+ this.statusText));
};
request.open('GET', url);
request.send();
});
}
ES2015 Promise
httpGet(
'https://api.github.com/search/repositories?q=angular’
)
.then(
function (value) {
console.log('Contents: ' + value);
},
function (reason) {
console.error('Something went wrong', reason);
});
ES2015 Promise
- Promise.reject()
ES2015 Promise
• Chaining Promises
asyncFunc()
.then(function (value1) {
return 123;
})
.then(function (value2) {
console.log(value2); // 123
});
ES2015 Promise
• Chaining Promises
ES2015 Promise
asyncFunc1()
.then(function (value1) {
asyncFunc2()
.then(function (value2) {
···
});
})
ES2015 Promise
asyncFunc1()
.then(function (value1) {
return asyncFunc2();
})
.then(function (value2) {
···
});
ES2015 Promise
function asyncFunc1() {
return new Promise(function(resolve, reject) {
asyncFunc2().then(function(data) {
// extra work with data
resolve(data);
}).catch(reject);
});
}
ES2015 Promise
function asyncFunc1() {
return asyncFunc2().then(function(data) {
// extra work with data
return data;
});
}
ES2015 Promise
• Promise.all
• Promise and the Event loop
• Promise only return single value.
function f() {
return Promise.resolve(1);
}
Async/Await
f();
Async/Await
function f() {
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000)
});
// SyntaxError
const result = await promise;
}
f();
Async/Await
• Async methods
class User {
constructor(username) {
this.username = username;
}
async getUser() {
const response = await fetch(
`https://api.github.com/search/users?q=${this.username}`
);
return await response.json();
}
}
• Error handling
async function getUser(username) {
try {
const response = await fetch(
`https://api.github.com/search/users?q=${username}`
);
return await response.json();
} catch(e) {
throw e;
}
}
getUser('tiep’)
.then(res => console.log(res))
.catch(err => console.warn(err));
Async/Await
(async () => {
x += await r5();
console.log(x);
})();
Async/Await
(async () => {
const y = await r5();
x += y;
console.log(x);
})();
Async/Await
• Too Sequential
async function getBooksAndAuthor(authorId) {
const books = await fetchAllBook();
const author = await fetchAuthorById(authorId);
return {
author,
books: books.filter(book => book.authorId === authorId),
};
}
Async/Await
• Too Sequential
async function getBooksAndAuthor(authorId) {
const bookPromise = fetchAllBook();
const authorPromise = fetchAuthorById(authorId);
const books = await bookPromise;
const author = await authorPromise;
return {
author,
books: books.filter(book => book.authorId === authorId),
};
}
DEMO
Q&A
References
• http://speakingjs.com/es5/index.html
• http://exploringjs.com/es6/index.html
• http://exploringjs.com/es2016-es2017/ch_async-functions.html
• http://exploringjs.com/es2018-es2019/toc.html
• http://eloquentjavascript.net/
• https://github.com/getify/You-Dont-Know-JS#titles
THANK YOU
www.nashtechglobal.com