Skip to content

Instantly share code, notes, and snippets.

@flavioribeirojr
Created December 29, 2018 14:39
Show Gist options
  • Save flavioribeirojr/bacc4bd4d7115fe63760759e34295f56 to your computer and use it in GitHub Desktop.
Save flavioribeirojr/bacc4bd4d7115fe63760759e34295f56 to your computer and use it in GitHub Desktop.
function createCart() {
const data = {
items: []
}
function onItemAddedToCart() {
alert(`The cart has ${data.items.length} items`)
}
function addItem(item) {
new Promise((resolve, reject) => {
setTimeout(() => {
data.items.push(item)
resolve()
}, 500)
}).then(onItemAddedToCart)
}
return {
addItem
}
}
const myCart = createCart()
myCart.addItem('Notebook') // The cart has 1 item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment