Skip to content

Instantly share code, notes, and snippets.

@flavioribeirojr
Last active December 29, 2018 13:35
Show Gist options
  • Save flavioribeirojr/4f705cd11cac6dda0edeebbe7566e3f1 to your computer and use it in GitHub Desktop.
Save flavioribeirojr/4f705cd11cac6dda0edeebbe7566e3f1 to your computer and use it in GitHub Desktop.
class Cart {
constructor() {
this.cart = []
}
onItemAddedToCart() {
alert(`The cart has ${this.cart.length} items`)
}
addItemToCart(item) {
new Promise((resolve, reject) => // Simulação de requisição http
setTimeout(() => {
this.cart.push(item)
resolve()
}, 500)
)
.then(this.onItemAddedToCart)
}
}
const cart = new Cart()
cart.addItemToCart('Notebook') // this.cart is undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment