Jump to content
McKay Development

Recommended Posts

Posted

 

 

I have a Bot class and an async method for logIn, are there any better way for writing this method?

 

completeLogin(botDetails, useLoginKey) {
    this.user.logOn(this.details(botDetails, useLoginKey));

    return new Promise((done, reject) => {
        const cxt = this;

        async function success(_, cookies) {
            try {
                await cxt.setCookies(cookies);
                cxt.user.off('error', failed);
                done();
            } catch(err) {
                reject(err);
            }
        }

        function failed(err) {
            cxt.user.off('webSession', success);
            reject(err);
        }

        cxt.user.once('webSession', success);
        cxt.user.once('error', failed);
    });
}

 

Posted

 

This logic will be written in the constructor. Something like this :

constructor(accountName) {
    this.setCookies = util.promisify(this.manager.setCookies).bind(this.manager);

    this.user.on('error', this.logOut);
    this.user.on('loginKey', loginKey => db.findOne('bot', { accountName }, { loginKey }));
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...