Call Back
HTML
<ul id="posts"></ul>
JavaScript
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.getElementById('posts').innerHTML = output;
}, 1000);
}
function createPost(post, callback) {
setTimeout(() => {
posts.push(post);
callback();
}, 2000);
}
createPost({ title: 'Post Three', body: 'This is post three' }, getPosts);
Code from Brad Traversy's tutorial:
Async JS Crash Course - Callbacks, Promises, Async Await
https://www.youtube.com/watch?v=PoRJizFvM7s