Async Method Chaining? [duplicate]

2019-08-27 22:51发布

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()

1条回答
乱世女痞
2楼-- · 2019-08-27 23:37

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);
查看更多
登录 后发表回答