This question already has an answer here:
I'm trying to create a class with async method chaining. However, I'm still setting the new values before I fully fetch the data from the database. Am I missing something?
I also don't want to use any third-party modules.
/* Class */
class UserDB {
constructor() {
this.user = {}
this.user.username = null
}
set(name, value) {
this.user[name] = value
return this
}
update() {
return new Promise((resolve, reject) => {
const db = MongoConnection.client.db('database').collection('users')
db.updateOne({ _id: this.user._id }, { $set: this.user}, (err, result) => {
if (err) reject(err)
resolve(this)
})
})
}
findByEmail(email) {
return new Promise((resolve, reject) => {
const db = MongoConnection.client.db('database').collection('users')
db.findOne({ email: email }, (err, result) => {
if (err) reject(err)
this.user = result
resolve(this)
})
})
}
}
module.exports = UserDB
/*execution*/
new UserDB().findByEmail('email@email.com')
.set('username', 'new_name')
.update()
Thats indeed interesting. You could overload the returned Promise with methods that attach the operation to a
.then
chain:Then use that in your custon class:
That way you can do: