The HTML file has a empty body.
This is the first code without editing anything
const posts = [
{ title: 'Post One', body: 'This is post one'},
{ title: 'Post two', body: 'This is post two'}
];
function getPosts(){
setTimeout(()=>{
let output = '';
posts.forEach((post,index) => {
output += `<li>${post.title}</li>`;
});
document.body.innerHTML = output;
},1000);
}
function createPost(post){
setTimeout(()=>{
posts.push(post);
}, 2000);
}
getPosts()
createPosts({title:'Post three', body:'This is post three'});
The output we get is
Post One
Post two
We are not seeing 'Post Three' as expected.
Now we will pass getPosts function as an argument to the createPost function
const posts = [
{ title: 'Post One', body: 'This is post one'},
{ title: 'Post two', body: 'This is post two'}
];
function getPosts(){
setTimeout(()=>{
let output = '';
posts.forEach((post,index) => {
output += `<li>${post.title}</li>`;
});
document.body.innerHTML = output;
},1000);
}
function createPost(post,callback){
setTimeout(()=>{
posts.push(post);
callback();
}, 2000);
}
createPosts({title:'Post three', body:'This is post three'},getPosts);
Now the output is completely blank. Even Post One and Post Two is not added to DOM let alone Post Three which we expected in this case
Where am I going wrong?