-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmore-promises.html
59 lines (52 loc) · 1.18 KB
/
more-promises.html
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
<!DOCTYPE html>
<html>
<head>
<title>
Promises
</title>
</head>
<body>
<script>
const posts = [
{ "name" : "Some Title", "author" : "James HD", id : 2},
{ "author" : "Martin White", "name" : "Leading SCRUM teams" , id: 1},
{ "name" : "A cloud by any other name", "author" : "Matt Bradburn", id: 3}
];
const authors = [
{ name: "James HD", "job": "Programmer"},
{ name : "Martin White", job: "Product Owner"},
{ name : "Matt Bradburn", job: "Architect"}
];
function getPostById(id) {
return new Promise((resolve, reject) => {
setTimeout(function() {
const post = posts.find(post => post.id === id);
if (post) {
resolve(post);
}
reject (Error('No post was found'));
}, 500);
});
}
function hydrateAuthor(post) {
return new Promise((resolve, reject) => {
const authorDetails = authors.find(person => person.name == post.author);
if (authorDetails) {
post.author = authorDetails;
resolve(post);
} else {
reject(Error('author not found!'));
}
});
}
getPostById(3)
.then(post => {
return hydrateAuthor(post);
})
.then(post => {
console.log(post);
})
.catch(err => console.error(err));
</script>
</body>
</html>