devlev1980
1/12/2018 - 1:00 PM

Callback_functions

// alert(1);
const posts = [
  {title:'Post One',body:'This is post one'},
  {title:'Post Two',body:'This is post two'},
  // {title:'Post Three',body:'This is post three'}
]
//Syncronize call functions

// function createPost(post) {
//   setTimeout(function() {
//     posts.push(post)
//   },2000)
// }
// function getPosts() {
//   setTimeout(function() {
//     let output= '';
//     posts.forEach(post => {
//       output+=`<li>${post.title}</li>`
//     });
//     document.body.innerHTML = output;
//   },1000)
// }

// createPost({title:'Post Three',body:'This is post three'});
//  getPosts();

// Callback function / Async
function createPost(post,callback) {
  setTimeout(function() {
    posts.push(post);
    callback();
  },2000)
}
function getPosts() {
  setTimeout(function() {
    let output= '';
    posts.forEach(post => {
      output+=`<li>${post.title}</li>`
    });
    document.body.innerHTML = output;
  },1000)
}
createPost({title:'Post Three',body:'This is post three'},getPosts);