Last active
April 8, 2018 02:26
-
-
Save rauschma/e1ee250122e9169b8bf7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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