-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (47 loc) · 2.59 KB
/
Copy pathindex.js
File metadata and controls
62 lines (47 loc) · 2.59 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
// Week 3 demo – Promises & async/await (worksheet for class)
// JSONPlaceholder only (reliable in the browser). Session plan may mention Open Notify —
// same async ideas, different URL.
const USER_URL = "https://jsonplaceholder.typicode.com/users/1";
const POST_URL = "https://jsonplaceholder.typicode.com/posts/1";
const TODO_URL = "https://jsonplaceholder.typicode.com/todos/1";
// =============================================================================
// Async/await – simple usage
// =============================================================================
// Task: Load USER_URL with async/await
async function getUser() {}
// Next: Exercise 1
// =============================================================================
// Why use Promises? :: Callback Hell
// =============================================================================
// Show Callback Hell example in https://www.npmjs.com/package/q
// =============================================================================
// Promise consumption
// =============================================================================
// Task: Load one of the resources (e.g. USER_URL); show success or error on the page using .then / .catch only.
// Next: Chaining examples
// Next: Exercise 2
// =============================================================================
// Promise creation
// =============================================================================
// Task: Create a Promise that resolves after 1 second and shows "It worked" on the page.
// Task: Create demoOrderPizza: a pizza-order Promise — after a 'baking' delay it either resolves with a pizza you can eat (show that on the page) or rejects if baking failed (show the failure on the page).
// Next: Exercise 3
// Next: Exercise 4
// =============================================================================
// Back to async/await (try / catch)
// =============================================================================
// Task: improve getUser to use try/catch to handle errors and show the error on the page.
// Next: Exercise 5
// =============================================================================
// Promise.all
// =============================================================================
// Next: Exercise 6
// =============================================================================
// (Optional) Infinite loop via Promises
// =============================================================================
function promiseLoop() {
return Promise.resolve().then(() => {
console.log("tick");
return promiseLoop();
});
}