-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.js
More file actions
executable file
·39 lines (35 loc) · 1.01 KB
/
Copy pathcallback.js
File metadata and controls
executable file
·39 lines (35 loc) · 1.01 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
const posts = [
{title: 'Post One', body: 'This is a post 1'},
{title: 'Post Two', body: 'This is a post 2'},
]
/**
* Print in the DOM all posts, each one wait 1 second to be posted.
*/
function getPosts() {
setTimeout(() => {
let output = '';
posts.forEach((post, index) => {
output += `<li>${post.title}</li>`;
});
document.body.innerHTML = output;
}, 1000);
}
/**
*
* @param { JSON Object } post
* @param { Function } callback
* The post will be added to posts and then the callback will be called
* The function usual pass is the getPost to refresh the posts in DOM.
*
*/
function createPost(post, callback) {
setTimeout(() => {
let newPost = {title: post.title, body: post.body };
posts.push(newPost);
callback();
}, 2000);
}
// Execution
getPosts();
createPost({title: 'oi', body: 'ahahah', lixo: 'Will I overflow?'}, getPosts);
createPost({title: 'de novo', body: 'hihihi', lixo: 'Will I overflow?'}, getPosts);