-
Notifications
You must be signed in to change notification settings - Fork 1
/
04_Async_functions.js
executable file
·69 lines (48 loc) · 2.18 KB
/
04_Async_functions.js
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
63
64
65
66
67
68
69
// Base async function:
const fetchText = () => new Promise(resolve => { setTimeout(() => resolve('ES8'), 2000); });
const showText = async () => {
const fetchedText = await fetchText();
console.log(`This is a feature of ${fetchedText}`);
};
showText(); // This is a feature of ES8
// Work sync with async functionality:
const fetchText = () => new Promise(resolve => { setTimeout(() => resolve('ES8'), 2000); });
const showText = () => {
const fetchedText = fetchText();
console.log(`This is feature a of ${fetchedText}`);
};
showText(); // This is a feature of [object Promise]
// Async function always return Promise
// We can handle result:
const fetchText = () => new Promise(resolve => { setTimeout(() => resolve('ES8'), 2000); });
const showText = async () => {
const fetchedText = await fetchText();
return `This is feature a of ${fetchedText}`;
};
showText().then(data => console.log(data)); // This is a feature of ES8
// if async function return simple value,
// this value still be in Promise
const fetchText = () => new Promise(resolve => { setTimeout(() => resolve('ES8'), 2000); });
const showText = async () => {
const fetchedText = await fetchText();
return 10;
};
console.log(showText()); // Promise
showText().then(data => console.log(data)); // 10
// Execute of 2 and more async functions:
const fetchDescrText = () => new Promise(resolve => { setTimeout(() => resolve('This is a feature of'), 2000); });
const fetchEsText = () => new Promise(resolve => { setTimeout(() => resolve('ES8'), 2000); });
const showText = async () => {
const fetchedDescrText = await fetchDescrText();
const fetchedEsText = await fetchEsText();
return `${fetchedDescrText} ${fetchedEsText}`;
};
// Receive result after ~4000ms
showText().then(data => console.log(data)); // This is a feature of ES8
// Parallel execute of async functions:
const showText = async () => {
const [fetchedDescrText, fetchedEsText] = await Promise.all([fetchDescrText(), fetchEsText()]);
return `${fetchedDescrText} ${fetchedEsText}`;
};
// Receive result after ~2000ms
showText().then(data => console.log(data)); // This is a feature of ES8