Created
December 29, 2018 14:39
-
-
Save flavioribeirojr/bacc4bd4d7115fe63760759e34295f56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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