Skip to content

Instantly share code, notes, and snippets.

@samuraijane
Created July 5, 2022 06:15
Show Gist options
  • Save samuraijane/0052949441e3ade7e6400eb4e58bf858 to your computer and use it in GitHub Desktop.
Save samuraijane/0052949441e3ade7e6400eb4e58bf858 to your computer and use it in GitHub Desktop.
// callback functions can help us deal with asynchronous code but it can also get messy if we have multiple sequential callbacks (not shown here)
var shoppingList = ['apples', 'biscuits', 'cabbage'];
function addItem(item, callback) {
setTimeout(() => {
shoppingList.push(item);
console.log("Item added to shopping list");
callback();
}, 200);
}
function getAllItems() {
setTimeout(() => {
console.log('Shopping List Items:');
console.log(shoppingList);
}, 100);
}
addItem('dip', getAllItems);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment