CALLBACK

by 1eddy87

HTML

<div id="test">Loading</div>

JavaScript

/* CALLBACK */

const posts = [{
    title: 'post one',
    body: 'this is post one'
  },
  {
    title: 'post two',
    body: 'this is post two'
  },
]

function getPosts() {
  setTimeout(() => {
    let output = ''; // document.createElement('div');
    posts.forEach((post) => {
      /* console.log(post) */

      output += `<div>${post.title}</div>`;
    });

    /* console.log(output) */
    
    document.getElementById('test').innerHTML = output;
  }, 1000);
}

function createPost(post, callback) {
  setTimeout(() => {
    posts.push(post);
    callback();
  }, 2000);
}

/* getPosts(); */

createPost({
  title: 'post three',
  body: 'this is post three'
}, getPosts);

/* CALLBACK */