-
Notifications
You must be signed in to change notification settings - Fork 0
/
promises.js
37 lines (30 loc) · 1.03 KB
/
promises.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
const posts = [
{title: 'Post One', body: 'This is post one'},
{title: 'Post Two', body: 'This is post two'},
];
function getPosts() {
setTimeout(() => {
//get the posts and put them on the page
let output = '';
posts.forEach((post, index) => {
output += `<li>${post.title}</li>`;
});
document.body.innerHTML = output;
}, 1000);
}
function createPost(post){ //function call back should be called right after the post is pushed
return new Promise((resolve, reject) => {
setTimeout(() => {
posts.push(post);
const error = true; //if error is sent to true it is going to reject, if it is false then it will be pased
if(!error) {
resolve();
}else{
reject('Error. Something went wrong')
}
}, 2000);
})
}
createPost({title: 'Post Three', body: 'This is post three'})
.then(getPosts)
.catch(err => console.log(err));