Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active April 8, 2018 02:26
Show Gist options
  • Save rauschma/e1ee250122e9169b8bf7 to your computer and use it in GitHub Desktop.
Save rauschma/e1ee250122e9169b8bf7 to your computer and use it in GitHub Desktop.
// Anti-pattern: creating a new promise instead of continuing an existing chain
insert() { // method inside ES6 class
return new Promise((resolve, reject) => {
this.db.collection(this.collection).insert(this.fields)
.then((modelDocument) => {
this.fields = modelDocument;
this.notifyObservers({event: "created", model: this});
resolve(modelDocument)
}).catch((err) => {
reject(err)
})
});
}
// Better:
insert() {
return this.db.collection(this.collection).insert(this.fields)
.then(modelDocument => {
this.fields = modelDocument;
this.notifyObservers({event: "created", model: this});
return modelDocument;
});
}
// More information on promises: http://www.2ality.com/2014/10/es6-promises-api.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment