-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
67 lines (55 loc) · 2.25 KB
/
index.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
const bookListEl = document.querySelector("#books");
const topicHeaderEl = document.querySelector("#topic")
const searchInputEl = document.querySelector("#search-input");
const searchFormEl = document.querySelector("#search-form");
// async arrow function
const fetchFrom = async (url) => {
try { // attempt error-prone code
const response = await fetch(url) // returns
const data = await response.json(); // convert JSON to JS object
return data;
}
catch (error) { // prevent an error from crashing your code
console.log(error);
}
}
// Async Function Declaration
async function findBookAbout(searchTerm) {
const url = `https://www.googleapis.com/books/v1/volumes?q=${searchTerm}`;
const data = await fetchFrom(url); // make blocking with await
bookListEl.innerHTML = "";
data.items.forEach(item => {
const li = document.createElement("li")
li.innerText = item.volumeInfo.title;
bookListEl.append(li);
})
}
searchFormEl.addEventListener('submit', (e) => {
e.preventDefault();
const searchTerm = searchInputEl.value;
topicHeaderEl.innerText = `Books about ${searchTerm}`
// If the search term has spaces, we need to use %20 in the query string
searchTerm = searchTerm.replace(" ", "%20")
// findBookAbout is an async function, but we don't need to
// wait for it to resolve since it doesn't return anything
findBookAbout(searchTerm);
searchInputEl.value = '';
})
console.log("this did not wait!")
/*
What we know about async/await?
- they are keywords that determine how functions are executed
- can be used on functions
- cleaner way to write async code that uses Promises
What does .then do?
- defines a callback method that is executed once the promise is fulfilled
- the callback is given the resolved value of the promise
- catch defines a method that is executed once the promise is rejected
What does async/await do?
- await turns a function that returns a Promise into blocking code
- await waits for a Promise to resolve and then returns that resolved value
- async is added before the function declaration
- Any value returned from an async function is wrapped in a Promise
How do we handle errors?
- Use the `try` / `catch` syntax
*/