-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.Promises.js
More file actions
70 lines (61 loc) · 1.28 KB
/
1.Promises.js
File metadata and controls
70 lines (61 loc) · 1.28 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
63
64
65
66
67
68
69
70
/*
* Author: Marc Ciruelos Santos
* Date: 21-02-2024
* Description: Promise => Example of a promise management.
*/
const books = [
{
id: 1,
title: "Thinking, fast and slow",
author: "Daniel Kahneman",
price: 12.5,
format: "paperback",
genres: ["Education", "Psychology"],
},
{
id: 2,
title: "Deep work. Rules for focused success in a distracted world",
author: "Cal Newport",
price: 25.69,
format: "hardcover",
genres: ["Education", "Business"],
},
{
id: 3,
title: "Rules for life: An antidote to chaos",
author: "Jordan B. Peterson",
price: 12.86,
format: "paperback",
genres: ["Business", "Study Skills"],
},
];
// Example usual function
const getBooksNoDelayed = () => {
return books;
};
console.log(getBooksNoDelayed());
// Async - Delay forced (1500)
const getBooks = () => {
setTimeout(() => {
return books;
}, 1500);
};
console.log(getBooks()); // Undefined
// Async - Delay forced (1500)
const getBooksAsync = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(books);
}, 1500);
});
};
// [object Promise]
console.log(getBooksAsync());
// Data
console.log(
getBooksAsync()
.then((data) => {
console.log(data);
})
.catch((err) => {})
);