2

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()
Owenimus
  • 21
  • 4
  • Async functions return Promises, so you can't really chain them like that without changing the Promise class itself. Why do you think you need these functions to be async? – Lee Daniel Crocker Nov 12 '18 at 21:19
  • well... You can chain async methods like this, but, it would have to work in such a way that what you're actually doing is setting up a chain of things that you want to occur. For xmaple, .set() wouldn't be able to directly access the results of `fetchData` because `set()` will run before `fetchData()` has completed its async action. The setup of such a system would very much follow the existing information you've found WRT chaining using the `this` keyword. – Kevin B Nov 12 '18 at 21:20
  • See also https://stackoverflow.com/a/43880581/1048572 – Bergi Nov 12 '18 at 21:21
  • @LeeDanielCrocker I was thinking more of using callbacks. I seen many modules use chained methods for async operations for manipulating the database. – Owenimus Nov 12 '18 at 21:22
  • Right, there's db modules that let you build a query by chaining a few methods, then execute the query by calling .exec(). That would be done using the `this` keyword or similar setup. – Kevin B Nov 12 '18 at 21:22
  • Yes you would have to implement the similar methodology like Promises uses ie `then()`,`finally()`, `catch()`, etc where the callbacks passed are stored and then called in turn whenever the async operation is done – Patrick Evans Nov 12 '18 at 21:23
  • @PatrickEvans Not just "similar to" promises. You should just make your class a wrapper around an actual promise. – Bergi Nov 12 '18 at 22:45
  • @PatrickEvans I'm not sure how that's done. Is there a name for that? I'm still learning. – Owenimus Nov 12 '18 at 22:49

1 Answers1

2

Thats indeed interesting. You could overload the returned Promise with methods that attach the operation to a .then chain:

 class AsyncChainable {
   constructor() {
    this._methods = {};

    const that = this;
    for(const key of  Object.getOwnPropertyNames(Object.getPrototypeOf(this))) {
     this._methods[key] = function(...args) {
       return that.chain(this.then(() => that[key](...args)));
    }
  }

  chain(promise) { return Object.assign(promise, this._methods); }
}

Then use that in your custon class:

 class User extends AsyncChainable {
   login() {
    return this.chain((async () => {
     // some logic
    })());
  }
}

That way you can do:

 (new User).login().login().then(console.log);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151