-
Notifications
You must be signed in to change notification settings - Fork 5
/
23.1_async_await.js
105 lines (97 loc) · 3.47 KB
/
23.1_async_await.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* ========================================================
* Async/Await
* ========================================================
* 'async/await' is a modern way to write asynchronous code in JavaScript.
* It provides a more readable and maintainable syntax over callbacks and Promises.
*/
/**
* ========================================================
* 1. Basic Syntax
* ========================================================
* Declaring a function as 'async' makes it return a Promise implicitly.
* You can use 'await' within an 'async' function to pause the execution until a Promise is resolved or rejected.
*/
async function fetchData() {
const response = await fetch("https://api.example.com/data");
const data = await response.json();
return data;
}
// Invoke the function to see it in action
fetchData().then((data) => console.log(data));
/**
* ========================================================
* 2. Handling Errors
* ========================================================
* It's crucial to handle errors when dealing with asynchronous operations.
* 'try/catch' blocks are commonly used within 'async' functions for this purpose.
*/
async function fetchDataWithErrorHandling() {
try {
const response = await fetch("https://api.example.com/data");
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
return data;
} catch (error) {
console.error("Fetch Error:", error);
}
}
// Invoke the function to see error handling in action
fetchDataWithErrorHandling().catch((error) => console.error(error));
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* Error Propagation
* -----------------
* Throwing an exception within an 'async' function causes it to return a Promise that is rejected.
*/
async function failAsync() {
throw new Error("Failed");
// This is equivalent to: return Promise.reject(new Error('Failed'));
}
/**
* Concurrency with Promise.all()
* ------------------------------
* 'async/await' can work in tandem with Promise.all() to execute multiple asynchronous operations concurrently.
*/
async function fetchAllData() {
const [data1, data2] = await Promise.all([
fetch("https://api.example.com/data1").then((res) => res.json()),
fetch("https://api.example.com/data2").then((res) => res.json()),
]);
return { data1, data2 };
}
/**
* Using 'for-await-of' with Async Iterables
* -----------------------------------------
* The 'for-await-of' loop enables you to traverse through async iterables as though they were synchronous.
*/
async function handleAsyncIterable(asyncIterable) {
for await (const item of asyncIterable) {
console.log(item);
}
}
/**
* Non-Promise Asynchronous Operations
* ----------------------------------
* While 'await' is designed to work with Promises, it can also be used with non-Promise values.
* When you 'await' a non-Promise value, it's returned instantly.
*/
async function notReallyAsync() {
const value = await 42;
return value; // 42, instantly
}
/**
* Running Async Functions Immediately
* -----------------------------------
* You can immediately invoke an async function using an IIFE (Immediately Invoked Function Expression).
*/
(async function () {
const data = await fetchData();
console.log(data);
})();