-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathES9.js
More file actions
62 lines (51 loc) · 1.3 KB
/
ES9.js
File metadata and controls
62 lines (51 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 1. Asynchronous Iteration
async function* asyncGenerator() {
yield 'Hello';
yield 'World';
}
// Example usage
(async () => {
for await (const value of asyncGenerator()) {
console.log(value); // Hello, World
}
})();
// Another example
async function fetchData() {
const data = [1, 2, 3];
for await (const item of data) {
console.log(item); // 1, 2, 3
}
}
// Example usage
fetchData();
// 2. Rest/Spread Properties
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
// Example usage
console.log(obj2); // { a: 1, b: 2, c: 3 }
const obj3 = { d: 4, ...obj2 };
// Example usage
console.log(obj3); // { d: 4, a: 1, b: 2, c: 3 }
// 3. Promise.prototype.finally
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Done!'), 1000);
});
// Example usage
promise
.then(result => {
console.log(result); // Done!
})
.finally(() => {
console.log('Promise has been settled.'); // Promise has been settled.
});
// Another example
const failedPromise = new Promise((_, reject) => {
setTimeout(() => reject('Error!'), 1000);
});
failedPromise
.catch(error => {
console.log(error); // Error!
})
.finally(() => {
console.log('Promise has been settled.'); // Promise has been settled.
});