-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_book.js
33 lines (29 loc) · 957 Bytes
/
2_book.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
async function getBookDetails(query) {
const url = `https://openlibrary.org/search.json?q=${query}`;
try {
const response = await fetch(url);
if(response.ok) {
const bookData = await response.json();
const bookDetails = {
name: bookData.docs[0].title,
publish_year: bookData.docs[0].first_publish_year,
author: bookData.docs[0].author_name,
};
return bookDetails;
} else {
console.error(`Error: ${response.status}`);
return null;
}
} catch(error) {
console.error(`An error ocurred: ${error}`);
return null;
}
}
getBookDetails("do android dream")
.then(bookDetails => {
if(bookDetails) {
console.log(`Name: ${bookDetails.name}`);
// console.log(`Published: ${bookDetails.publish_year}`);
console.log(`Author: ${bookDetails.author}`);
}
})