-
Notifications
You must be signed in to change notification settings - Fork 0
/
await.js
44 lines (30 loc) · 1.13 KB
/
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
function getEmployeesData() {
console.log("Start of getEmployeesData()");
const URL = "https://dummyjson.com/products";
const response = fetch(URL);
response
.then((res) => res.json())
.then((json) => json.products)
.then((products) => products[0])
.then((firstProduct) => firstProduct.title)
.then((title) => console.log(title));
console.log("End of getEmployeesData()");
}
getEmployeesData();
// // =======================================================================
// Await is a keyword that is used inside an async function.
// Await waits for the promise to resolve and returns the result.
// we use the await keyword to wait for the response to come back
// before moving on to the next line of code.
async function getEmployeesData() {
console.log("Start of getEmployeesData()");
const URL = "https://dummyjson.com/products";
const response = await fetch(URL);
const json = await response.json();
const products = json.products;
const firstProduct = products[0];
const title = firstProduct.title;
console.log(title);
console.log("End of getEmployeesData()");
}
getEmployeesData();